繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM