namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
public MainWindow()
{
InitializeComponent();
}
private void startButton_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);
//problem
storyboard.Begin();
}
}
$ 我的錯誤(「System.InvalidOperationException」類型的未處理的異常在PresentationFramework.dllC#WPF錯誤(System.InvalidOperationException)
發生其他信息:無法解析的屬性路徑的所有屬性引用'(canvas.left)'。驗證適用的對象是否支持屬性。)
有人可以幫助我嗎?
你可能需要完整的命名空間 – SLaks
你的意思完整的命名空間是什麼? –
嘗試使用反映實際類型/屬性的情況,例如'(Canvas.Left)'。 –