原文: WPF旋轉的界面實作
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/yangyisen0713/article/details/18215349
在WPF中可以做出旋轉的界面,這樣不僅效果好看,而且節省界面和代碼處理。
xaml代碼如下:
<Window x:Class="V3ViewApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="旋轉界面" Height="360" Width="450" Background="Black" WindowState="Normal" WindowStartupLocation="CenterScreen">
<Window.Resources>
<sys:Double x:Key="grdSize">200</sys:Double>
</Window.Resources>
<Grid >
<Viewport3D ClipToBounds="True" HorizontalAlignment="Center" VerticalAlignment="Center" Height="500" Width="500">
<Viewport3D.Camera>
<PerspectiveCamera LookDirection="0,0,-1" Position="0,0,300"/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>
<ModelVisual3D.Content>
<AmbientLight Color="Transparent"/>
</ModelVisual3D.Content>
<ModelVisual3D.Children>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-50,50,-50 -50,-50,-50 -50,-50,50 -50,50,50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Yellow" Width="{StaticResource grdSize}" Height="{StaticResource grdSize}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-50,50,50 -50,-50,50 50,-50,50 50,50,50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Red" Height="{StaticResource grdSize}" Width="{StaticResource grdSize}">
<StackPanel Margin="4">
</StackPanel>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="50,50,50 50,-50,50 50,-50,-50 50,50,-50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Green" Width="{StaticResource grdSize}" Height="{StaticResource grdSize}">
<Canvas>
<Ellipse />
</Canvas>
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="50,50,-50 50,-50,-50 -50,-50,-50 -50,50,-50"
Normals="0,0,1 0,0,1 0,0,1 0,0,1"
TriangleIndices="0,1,2 0,2,3"
TextureCoordinates="0,0 0,1 1,1 1,0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Brush="Transparent" Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<Grid Background="Blue" Height="{StaticResource grdSize}" Width="{StaticResource grdSize}">
</Grid>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
</ModelVisual3D.Children>
<ModelVisual3D.Transform>
<RotateTransform3D CenterX="0" CenterY="0" CenterZ="0">
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="0" Axis="0,1,0" x:Name="ar"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
</ModelVisual3D>
</Viewport3D.Children>
</Viewport3D>
</Grid>
</Window>
CS代碼如下:
public MainWindow()
{
InitializeComponent();
// 設定快捷鍵
KeyBinding forwBind = new System.Windows.Input.KeyBinding();
forwBind.Command = new ForwCommand();
forwBind.CommandParameter = ar;
forwBind.Key = System.Windows.Input.Key.Right;
this.InputBindings.Add(forwBind);
KeyBinding backBind = new System.Windows.Input.KeyBinding();
backBind.Command = new BackCommand();
backBind.CommandParameter = ar;
backBind.Key = System.Windows.Input.Key.Left;
this.InputBindings.Add(backBind);
this.Loaded += (k, k2) =>
{
};
}
}
/// <summary>
/// 向前移動
/// </summary>
public class ForwCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (parameter == null)
{
return false;
}
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
AxisAngleRotation3D rot = parameter as AxisAngleRotation3D;
DoubleAnimation d = new DoubleAnimation();
d.Duration = new Duration(TimeSpan.FromMilliseconds(800));
d.By = 90d;
rot.BeginAnimation(AxisAngleRotation3D.AngleProperty, d, HandoffBehavior.Compose);
}
}
/// <summary>
/// 向後移動
/// </summary>
public class BackCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (parameter == null)
{
return false;
}
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
AxisAngleRotation3D rot = parameter as AxisAngleRotation3D;
DoubleAnimation d = new DoubleAnimation();
d.By = -90d;
d.Duration = new Duration(TimeSpan.FromMilliseconds(800));
rot.BeginAnimation(AxisAngleRotation3D.AngleProperty, d, HandoffBehavior.Compose);
}
}
最後用鍵盤左右鍵實作界面切換,之後可以自己改成别的快捷鍵進行切換。
效果如圖:
