求大神看看我这个WPF后台代码哪里出问题了?代码是HEAD FIRST C# 第三版第一章的。

showmethepath 2017-09-21 11:30:57

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace mainpage
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Addenemy();
}

private void Addenemy()
{
ContentControl enemy = new ContentControl();
enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
AnimateEnemy(enemy, 0, playArea.ActualWidth -100, "(Canvas.Left)");
AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100), random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
playArea.Children.Add(enemy);
}
private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
{
Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
DoubleAnimation animation = new DoubleAnimation()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
};
Storyboard.SetTarget(animation, enemy);
Storyboard.SetTargetProperty(animation,new Propertypath(propertyToAnimate));
storyboard.Children.Add(animation);
storyboard.Begin();



}

}
}


这个代码是在点击按钮的时候,让一个Enemy的模板产生对象。
在VS 2017编译通过,点击按钮没有事件生成,求大神分析下。
...全文
160 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
showmethepath 2017-09-21
  • 打赏
  • 举报
回复
好像修改后,还是没得动。我感觉wpf应该写对了,我前面定义了一个动态资源的,名字叫做EnemyTemplate的ContentControl模板,不知道是不是动态的问题。 书上写的是静态,但是我按书上的步骤来得到的是动态,不知是不是vs版本的问题。
exception92 2017-09-21
  • 打赏
  • 举报
回复
DoubleAnimation animation = new DoubleAnimation() { From = from, To = to, Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6))), Name="animation " }; Storyboard.SetTargetName(enemy,"animation"); Storyboard.SetTargetProperty(animation,new Propertypath(propertyToAnimate)); storyboard.Children.Add(animation); storyboard.Begin(enemy);

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧