头先C#-第1章示例
我试图从头先C#中学到东西,但我在第一章中发现了这个错误: 其他信息:指定的Visual已经是另一个Visual的子级或CompositionTarget的根 我很确定代码与书中的完全相同,但我使用的是VS2015,示例是flr VS2013: 这是我的密码:头先C#-第1章示例,c#,C#,我试图从头先C#中学到东西,但我在第一章中发现了这个错误: 其他信息:指定的Visual已经是另一个Visual的子级或CompositionTarget的根 我很确定代码与书中的完全相同,但我使用的是VS2015,示例是flr VS2013: 这是我的密码: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows; 使用System.Win
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Windows.Media.Animation;
使用System.Windows.Threading;
名称空间拯救人类
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
随机=新随机();
DispatcherTimer enemyTimer=新DispatcherTimer();
DispatcherTimer targetTimer=新DispatcherTimer();
bool=false;
公共主窗口()
{
初始化组件();
enemyTimer.Tick+=enemyTimer\u Tick;
enemyTimer.Interval=TimeSpan.FromSeconds(2);
targetTimer.Tick+=targetTimer_Tick;
targetTimer.Interval=TimeSpan.FromSeconds(.1);
}
私有void TargetTimer_勾选(对象发送方,事件参数e)
{
progressBar.Value+=1;
如果(progressBar.Value>=progressBar.Maximum)
结束游戏();
}
私人游戏结束()
{
如果(!playArea.Children.Contains(游戏超文本))
{
enemyTimer.Stop();
targetTimer.Stop();
人形=假;
startButton.Visibility=可见性.Visibility;
游戏区。儿童。添加(游戏超文本);
}
}
私有void enemyTimer_Tick(对象发送方,事件参数e)
{
AddEnemy();
}
私有无效开始按钮单击(对象发送者,路由目标)
{
StartGame();
}
私有void StartGame()
{
Human.ishitsetvisible=true;
人形=假;
progressBar.Value=0;
开始按钮。可见性=可见性。折叠;
游戏区。儿童。添加(目标);
游戏区。儿童。添加(人类);
enemyTimer.Start();
targetTimer.Start();
}
私有无效加法器()
{
ContentControl敌人=新ContentControl();
defey.Template=资源[“EnemyTemplate”]作为ControlTemplate;
AnimateEnemy(敌人,0,playrea.ActualWidth-100,”(Canvas.Left)”;
AnimateEnemy(敌人,随机。下一个((int)playrea.ActualHeight-100),
random.Next((int)playrea.ActualHeight-100),“(Canvas.Top)”;
游戏区。儿童。添加(敌人);
}
私有void AnimateEnemy(ContentControl、double-from、double-to、string-propertyToAnimate)
{
情节提要情节提要=新建情节提要(){AutoReverse=true,RepeatBehavior=RepeatBehavior.Forever};
DoubleAnimation animantion=新的DoubleAnimation()
{
From=From,
To=To,
持续时间=新的持续时间(TimeSpan.FromSeconds(random.Next(4,6)),
};
故事板。设定目标(动物,敌人);
Storyboard.SetTargetProperty(动画,新的PropertyPath(propertyToAnimate));
故事板。儿童。添加(动画);
故事板。开始();
}
}
}
如果我对这两行进行注释,则不会出现错误
playArea.Children.Add(target);
playArea.Children.Add(Human);
不知怎的,这两个已经在xaml的画布中了。。或者我不知道如果你想学习C#你应该从一个不太复杂的例子开始,学习如何调试你的代码。
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;
using System.Windows.Threading;
namespace Save_the_humans
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
DispatcherTimer enemyTimer = new DispatcherTimer();
DispatcherTimer targetTimer = new DispatcherTimer();
bool humanCaptured = false;
public MainWindow()
{
InitializeComponent();
enemyTimer.Tick += enemyTimer_Tick;
enemyTimer.Interval = TimeSpan.FromSeconds(2);
targetTimer.Tick += TargetTimer_Tick;
targetTimer.Interval = TimeSpan.FromSeconds(.1);
}
private void TargetTimer_Tick(object sender, EventArgs e)
{
progressBar.Value += 1;
if (progressBar.Value >= progressBar.Maximum)
EndTheGame();
}
private void EndTheGame()
{
if(!playArea.Children.Contains(gameOverText))
{
enemyTimer.Stop();
targetTimer.Stop();
humanCaptured = false;
startButton.Visibility = Visibility.Visible;
playArea.Children.Add(gameOverText);
}
}
private void enemyTimer_Tick(object sender, EventArgs e)
{
AddEnemy();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
StartGame();
}
private void StartGame()
{
Human.IsHitTestVisible = true;
humanCaptured = false;
progressBar.Value = 0;
startButton.Visibility = Visibility.Collapsed;
playArea.Children.Add(target);
playArea.Children.Add(Human);
enemyTimer.Start();
targetTimer.Start();
}
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 animantion = new DoubleAnimation()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6))),
};
Storyboard.SetTarget(animantion, enemy);
Storyboard.SetTargetProperty(animantion, new PropertyPath(propertyToAnimate));
storyBoard.Children.Add(animantion);
storyBoard.Begin();
}
}
}
playArea.Children.Add(target);
playArea.Children.Add(Human);