Key_Down event not firing, How to move camera with keyboard arrows
My xaml code is below;
<Page x:Class="Ab3d.PowerToys.Samples.Objects3D.Model3DFactorySample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
xmlns:ab3d="clr-namespace:Ab3d.Controls;assembly=Ab3d.PowerToys"
xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"
Title="AllModelsSample"
MinWidth="600">
<Page.Resources>
<DiffuseMaterial x:Key="ObjectsMaterial" Brush="#247589"/>
</Page.Resources>
<Grid Name="MainGrid">
<Grid.Background>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Offset="0" Color="#033C62"/>
<GradientStop Offset="1" Color="#01131F"/>
</LinearGradientBrush>
</Grid.Background>
<Viewport3D Name="MainViewport">
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup x:Name="MainModel3DGroup"/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<AmbientLight Color="#333333"/>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
<cameras:SceneCamera Name="Camera1" Heading="30" Attitude="-10" Bank="0" Distance="10" ShowCameraLight="Always"/>
<!-- EventsSourceElement is set to MainGrid - this means that the MainGrid's mouse events are subscribed to - this enables camera rotation without the need to be over the rendered 3D element -->
<ab3d:MouseCameraController Name="MouseCameraController1" UsedMouseButton="Left" EventsSourceElement="{Binding ElementName=MainGrid}"/>
<ab3d:CameraControlPanel VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5" Width="150" Height="75"/>
<Viewport3D x:Name="ViewPort" Margin="0,-10,0,0" KeyDown="Window_KeyDown">
<Viewport3D.Camera>
<PerspectiveCamera x:Name="anakamera" FieldOfView="90"
Position="0.4,7,7.5"
LookDirection="0,-0.5,-1"
/>
</Viewport3D.Camera>
</Viewport3D>
<TextBlock FontWeight="Bold" FontSize="20" Foreground="Silver" Margin="10" VerticalAlignment="Top" HorizontalAlignment="Left" Text="3D objects created from code with Ab3d.Models.Model3DFactory"/>
</Grid>
</Page>
And my xaml.cs code is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
namespace Ab3d.PowerToys.Samples.Objects3D
{
/// <summary>
/// Interaction logic for Model3DFactorySample.xaml
/// </summary>
public partial class Model3DFactorySample : Page
{
public Model3DFactorySample()
{
InitializeComponent();
CreateModels();
}
ContainerUIElement3D barsContainer = new ContainerUIElement3D();
mamuller mamuller = new mamuller();
yuzeyler yuzeyler = new yuzeyler();
public static ModelUIElement3D modelOlustur(
Double x,
Double y,
Double z,
Double boy,
Double sag,
Double sol,
MeshGeometry3D mesh, Brush brush)
{
//Ab3d.Common.EventManager3D
ModelUIElement3D modelUIElement3D = new ModelUIElement3D()
{
Model = new GeometryModel3D(mesh, new DiffuseMaterial(brush))
};
Transform3DGroup transform = new Transform3DGroup();
//ScaleY is ranged between 0.0 : 1.0, for 0% to 100%
transform.Children.Add(new ScaleTransform3D(sol, sag, boy));//boyutlar x sol y sag z uzunlk
transform.Children.Add(
new TranslateTransform3D(x, y, z));///lokasyonları belırlenıyor x sag sol y asagı yukarı z derınlık
modelUIElement3D.Transform = transform;
return modelUIElement3D;
}
private void CreateModels()
{
Material material;
material = this.FindResource("ObjectsMaterial") as Material;
Brush renk = Brushes.Red;
ModelUIElement3D firin = modelOlustur(8.5, 1.6, -2.7, 0.1, 0.1, 0.1, (MeshGeometry3D)yuzeyler.Resources["fırın"], Brushes.Gray);
ModelUIElement3D sogutmaplt = modelOlustur(-7.6, 0.4, -2.7, 0.1, 0.1, 0.1, (MeshGeometry3D)yuzeyler.Resources["sogutmaplatformu"], Brushes.Gray);
ModelUIElement3D bar2 = modelOlustur(1, 2, 1, 0.1, 0.1, 0.1, (MeshGeometry3D)mamuller.Resources["12metreKOS-D"], renk);
ModelUIElement3D bar3 = modelOlustur(1, 2.077, 1, 0.1, 0.1, 0.1, (MeshGeometry3D)mamuller.Resources["12metreKOS-D"], Brushes.Yellow);
barsContainer.Children.Add(firin);
barsContainer.Children.Add(sogutmaplt);
barsContainer.Children.Add(bar2);
barsContainer.Children.Add(bar3);
//barsContainer.Children.Add(sogutmaplt);
//ViewPort.Children.Add(barsContainer);
MainViewport.Children.Add(barsContainer);
//string yon = "12metreKOS-D";
}
#region klavye mouse işlmleri
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.N)
{
anakamera.Position = new Point3D(0.4, 7, 7.5);
anakamera.LookDirection = new Vector3D(0, -0.5, -1);
}
#region normal kamera ayarları
if (e.Key == Key.Left)
{
RotateTransform3D cameraspin = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 10));
cameraspin.CenterX = 0;
cameraspin.CenterY = 开发者_如何转开发anakamera.Position.Y;
cameraspin.CenterZ = 0;
(anakamera.Transform as MatrixTransform3D).Matrix *= cameraspin.Value;
}
if (e.Key == Key.Right)
{
RotateTransform3D cameraspin = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), -10));
cameraspin.CenterX = 0;
cameraspin.CenterY = anakamera.Position.Y;
cameraspin.CenterZ = 0;
(anakamera.Transform as MatrixTransform3D).Matrix *= cameraspin.Value;
}
if (e.Key == Key.Up)
{
anakamera.Position = new Point3D(anakamera.Position.X, anakamera.Position.Y, anakamera.Position.Z - 0.1D);
}
if (e.Key == Key.Down)
{
anakamera.Position = new Point3D(anakamera.Position.X, anakamera.Position.Y, anakamera.Position.Z + 0.1D);
}
#endregion
}
}
#endregion
}
I am trying to use the keyboard arrows to move the camera but my Key_Down event isn't firing. is there anyway i can achieve this? Thank you
i used this ShowMoveButtons="{Binding ElementName=ShowMoveButtonsCheckBox, Path=IsChecked}"
. then the problem is solved
Try this:
<Page ... KeyDown="Window_KeyDown" Focusable="True">
Then you have to make sure, that your page has the KeyboardFocus.
精彩评论