我的C#WPF应用程序中有一个函数(Method)。我想从几个图像控件中使用此方法。
WPF代码
<Image x:Name="cardMPOne" HorizontalAlignment="Center" MouseDown="moveCard" Height="220" Margin="-300,20,0,0" VerticalAlignment="Top" Width="100"/>
C SHARP
private void moveCard(object sender, MouseButtonEventArgs e)
{
ThicknessAnimation move = new ThicknessAnimation();
move.From = new Thickness(sender.Margin.Left, sender.Margin.Top, 0, 0);
move.To = new Thickness(250, 250, 0, 0);
move.Duration = new Duration(TimeSpan.FromSeconds(0.25));
sender.BeginAnimation(MarginProperty, move);
}
我希望这种方法能够适用于每一个被击中并调用它的图像。
我也尝试了这个
private void moveCard(Image sender, MouseButtonEventArgs e){
ThicknessAnimation move = new ThicknessAnimation();
move.From = new Thickness(sender.Margin.Left, sender.Margin.Top, 0, 0);
move.To = new Thickness(250, 250, 0, 0);
move.Duration = new Duration(TimeSpan.FromSeconds(0.25));
sender.BeginAnimation(MarginProperty, move);
}
正常情况下没有显示错误。但是跑的时候。没有显示过载错误。 但是当我使用MouseDown =“moveCard(cardMPOne)”时,XAML视图显示错误。 令人困惑该怎么做。任何人的帮助表示赞赏..在此先感谢..
答案 0 :(得分:1)
你不能从xaml提供这样的参数。而是将处理程序更新为:
private void moveCard(object sender, MouseButtonEventArgs e)
{
Image image = sender as Image;
if(image ! =null)
{
ThicknessAnimation move = new ThicknessAnimation();
move.From = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0);
move.To = new Thickness(250, 250, 0, 0);
move.Duration = new Duration(TimeSpan.FromSeconds(0.25));
image .BeginAnimation(MarginProperty, move);
}
}