Article From:https://www.cnblogs.com/lonelyxmas/p/9689217.html
Interface implementation of WPF rotation

Copyright notice: This article is an original article for bloggers. It can not be reproduced without permission from bloggers. Https://blog.csdn.net/yangyisen0713/article/details/18215349

Rotating interfaces can be made in WPF, which not only looks good, but also saves interface and code processing.

xamlThe code is as follows:

<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="Rotating interface "Height =" 360 "Width =" 450 "Background =" Black "Windows State =" Normal "Windows Startup Location =" 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,0300 "/>< /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= "{Stati"CResource 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,00,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,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>

CSThe code is as follows:

  public MainWindow()
        {
            InitializeComponent();

            // Set up shortcut keysKeyBinding 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) =&gT;{};}}/ / / < sumMary>/ / / move forward/ / / < /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.FromMillisecond)S (800));D.By = 90d;Rot.BeginAnimation (AxisAngleRotation3D.AngleProperty,D, HandoffBehavior.Compose);}}/ / / < summary>/ / / backward movement/ / / &lT; /summary>Public class BackCommand: ICommand{Public bool CanExecute (objec)T 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);}}

Finally, the keyboard is used to switch the interface, and then switch to other shortcut keys to switch.

The effect is as follows:

Leave a Reply

Your email address will not be published. Required fields are marked *