C# 有没有一种方法可以使用WPF的箭头键移动图片?
我是WPF新手,我正在尝试使用2D Sprite gif文件制作RPG,用于向前、向后行走等动画 我在使用箭头键使原始图片向任何方向移动时遇到问题。以下是代码片段:C# 有没有一种方法可以使用WPF的箭头键移动图片?,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我是WPF新手,我正在尝试使用2D Sprite gif文件制作RPG,用于向前、向后行走等动画 我在使用箭头键使原始图片向任何方向移动时遇到问题。以下是代码片段: <UserControl x:Class="TextofTheWild2._0.Screen1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema
<UserControl x:Class="TextofTheWild2._0.Screen1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:local="clr-namespace:TextofTheWild2._0"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
FocusManager.FocusedElement = "{Binding ElementName=MyCanvas}">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Right" Height="450" VerticalAlignment="Top" Width="800" Background="#FFFCD8A8" KeyDown="Canvas_OnKeyDown" Focusable="True" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">
<Grid HorizontalAlignment="Left" Height="190" VerticalAlignment="Top" Width="410" Background="#FF00A800" Canvas.Left="390"/>
<TextBlock Height="40" Canvas.Left="149" TextWrapping="Wrap" Text="Press E to activate" Canvas.Top="40" Width="52" FontFamily="Microsoft Sans Serif" Visibility="Collapsed"/>
<Grid HorizontalAlignment="Left" Height="190" VerticalAlignment="Top" Width="27" Background="#FF00A800"/>
<Grid HorizontalAlignment="Left" Height="26" VerticalAlignment="Top" Width="85" Background="Black" Canvas.Left="27" Canvas.Top="35"/>
<Grid HorizontalAlignment="Left" Height="35" VerticalAlignment="Top" Width="112" Background="#FF00A800"/>
<Grid HorizontalAlignment="Left" Height="181" VerticalAlignment="Top" Width="27" Background="#FF00A800" Canvas.Top="269"/>
<Grid HorizontalAlignment="Left" Height="181" VerticalAlignment="Top" Width="22" Background="#FF00A800" Canvas.Left="778" Canvas.Top="269"/>
<Grid HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="800" Background="#FF00A800" Canvas.Top="370"/>
<Image Name="Green_Link" Height="93" VerticalAlignment="Top" Canvas.Left="480" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Green Link idle.gif" Source="Assets/Green Link idle.gif" Focusable="True"/>
<Image Name="Blue_Link" Height="93" VerticalAlignment="Top" Canvas.Left="390" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Blue Link idle.gif" Source="Assets/Blue Link idle.gif"/>
<Image Name="Red_Link" Height="93" VerticalAlignment="Top" Canvas.Left="305" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Red Link Idle.gif" Source="Assets/Red Link Idle.gif"/>
<Image Name="Purple_Link" Height="93" VerticalAlignment="Top" Canvas.Left="220" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Purple Link idle.gif" Source="Assets/Purple Link idle.gif"/>
</Canvas>
</UserControl>
在开始制作过渡动画之前,我想至少让图像在画布中移动。谢谢 看起来你把C代码放错地方了。XAML文件的开头是这样的:
到底是什么问题?执行时,箭头键不会移动程序上的图像。是否检查是否调用了Canvas_KeyDown?如果没有,请在画布上设置FocusManager.FocusedElement=“{Binding RelativeSource={RelativeSource Self}}”
。方法的名称似乎有问题。XAML中有Canvas_OnKeyDown,Screen1中定义了Canvas_KeyDown。你也可能把你的C#代码放错了文件-你的XAML表明你想要的代码隐藏文件是GameWindow.XAML.cs,而不是screen1我刚才重新阅读了这篇文章,似乎我发送了错误的XAML文件。我会很快编辑这篇文章。很抱歉。
public partial class Screen1 : UserControl
{
public Screen1()
{
InitializeComponent();
}
private void Canvas_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
Canvas.SetTop(Green_Link, Canvas.GetTop(Green_Link) + 10);
}
else if (e.Key == Key.Up)
{
Canvas.SetTop(Green_Link, Canvas.GetTop(Green_Link) - 10);
}
else if (e.Key == Key.Left)
{
Canvas.SetLeft(Green_Link, Canvas.GetLeft(Green_Link) - 10);
}
else if (e.Key == Key.Right)
{
Canvas.SetLeft(Green_Link, Canvas.GetLeft(Green_Link) + 10);
}
}
}