![](/img/trans.png)
[英]using WPF to display an image that can be moved by using the arrow keys in C#
[英]C# WPF - How to make object moving with arrow keys using behavior?
我對C#相當陌生,對MVVM甚至還很陌生,但是據我所知,最好是WPF可以與之配合使用,所以我將自己投入其中。 我想開始制作第一個非常簡單的游戲,但是一開始我就堅持了。 只是提一下:我一直在這里四處尋找,但我無法使其正常工作。 我創建了矩形,賦予它船形外觀,並希望使其移動。 這就是我得到的:C#-行為類
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Shapes;
namespace gierkaPierwszy.Zachowania
{
public class Zachowania : Behavior<Rectangle>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
AssociatedObject.Focus();
switch (e.Key)
{
case Key.Left:
if (Canvas.GetLeft(AssociatedObject) > 0) Canvas.SetLeft(AssociatedObject, Canvas.GetLeft(AssociatedObject) - 1);
break;
case Key.Right: Canvas.SetLeft(AssociatedObject,Canvas.GetLeft(AssociatedObject) + 1);
break;
case Key.Up:
if (Canvas.GetTop(AssociatedObject) > 0) Canvas.SetTop(AssociatedObject, Canvas.GetLeft(AssociatedObject) - 1);
break;
case Key.Down:
Canvas.SetTop(AssociatedObject, Canvas.GetTop(AssociatedObject) + 1);
break;
default:
return;
}
}
}
}
另外,因為它非常重要:我的xaml代碼:
<Window x:Class="gierkaPierwszy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:z="clr-namespace:gierkaPierwszy.Zachowania"
xmlns:local="clr-namespace:gierkaPierwszy"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="525">
<Canvas>
<Rectangle x:Name="Board" Width="525" Height="700">
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="C:\Users\User\Desktop\szkola c#\tło gry.jpg" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="SpaceShip" Width="50" Height="100" RenderTransformOrigin="0.1,0.54" Canvas.Top="400" Canvas.Left="250">
<i:Interaction.Behaviors>
<z:Zachowania/>
</i:Interaction.Behaviors>
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="C:\Users\User\Desktop\szkola c#\bronie 2d\PNG\Sprites\Ships\spaceShips_006.png" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
如果有人告訴我我哪里出了問題或幫助找出其他解決方案,那就太好了。
另外:很抱歉在某些地方使用非英語名稱,我已經習慣了編程標准,但有時會失敗。
問候 !
KeyEvents和MouseEvents(簡而言之)僅用於控制是否應該接收事件或其子對象。 您還可以創建附加到主窗口或某些父控件的行為,並使用目標控件(將是“ SpaceShip”)創建DependencyProperty。 然后,Window可以獲取事件,並且使用行為可以移動SpaceShip。 如果您有多個SpaceShip,您的行為將失敗。
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Shapes;
namespace gierkaPierwszy.Zachowania
{
public class Zachowania : Behavior<Window>
{
public static readonly DependencyProperty SpaceShipProperty
= DependencyProperty.Register("SpaceShip", typeof(Rectangle), typeof(Zachowania), new FrameworkPropertyMetadata(null));
public Rectangle SpaceShip
{
set { SetValue(SpaceShipProperty, value); }
get { return (Rectangle)GetValue(SpaceShipProperty); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (SpaceShip == null) return;
switch (e.Key)
{
case Key.Left:
if (Canvas.GetLeft(SpaceShip) > 0) Canvas.SetLeft(SpaceShip, Canvas.GetLeft(SpaceShip) - 1);
break;
case Key.Right: Canvas.SetLeft(SpaceShip,Canvas.GetLeft(SpaceShip) + 1);
break;
case Key.Up:
if (Canvas.GetTop(SpaceShip) > 0) Canvas.SetTop(SpaceShip, Canvas.GetLeft(SpaceShip) - 1);
break;
case Key.Down:
Canvas.SetTop(SpaceShip, Canvas.GetTop(SpaceShip) + 1);
break;
default:
return;
}
}
}
Xaml:
<Window x:Class="gierkaPierwszy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:z="clr-namespace:gierkaPierwszy.Zachowania"
xmlns:local="clr-namespace:gierkaPierwszy"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="525">
<i:Interaction.Behaviors>
<z:Zachowania SpaceShip="{Binding ElementName=SpaceShip}"/>
</i:Interaction.Behaviors>
<Canvas>
<Rectangle x:Name="Board" Width="525" Height="700">
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="C:\Users\User\Desktop\szkola c#\tło gry.jpg" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="SpaceShip" Width="50" Height="100" RenderTransformOrigin="0.1,0.54" Canvas.Top="400" Canvas.Left="250">
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="C:\Users\User\Desktop\szkola c#\bronie 2d\PNG\Sprites\Ships\spaceShips_006.png" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.