C#WPF - 如何使用行为使用箭头键移动对象?

时间:2018-01-30 09:15:21

标签: c# wpf xaml mvvm 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>

如果有人告诉我哪里出错或帮助找出其他解决方案,那将会很棒。

另外:对于某些地方的非英文名字,我很抱歉,我习惯了编程标准,但有时我会失败。

问候!

1 个答案:

答案 0 :(得分:0)

仅发送KeyEvents和MouseEvents(简称)以控制它是否应该接收事件或其子节点。 您还可以创建附加到主窗口或某些父控件的行为,并使用目标控件创建DependencyProperty,该控件将是“SpaceShip”。 然后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>