我是C#的新手,我一直在尝试设置一个基本的游戏(从一本书中),但由于出现以下错误,我将无法再运行
类型' System.Exception'的例外情况发生在mscorlib.dll但是 未在用户代码中处理
WinRT信息:元素已经是另一个元素的子元素。
其他信息:未检测到已安装的组件。
查看代码,我无法看到有什么问题;
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();
tagetTimer.Start();
}
查看文档大纲,这似乎也是正确的顺序;
对我来说唯一似乎是Xaml;
<Canvas x:Name="playArea" Grid.RowSpan="3" Margin="10,77,10,0" >
<StackPanel x:Name="human" Orientation="Vertical">
<Ellipse Fill="Purple" Height="10" Width="10" />
<Rectangle Fill="Purple" Height="25" Width="10"/>
</StackPanel>
<Rectangle x:Name="target" Height="53.149" Canvas.Left="1202.196" Stroke="Black" Canvas.Top="419.937" Width="56.008" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FF4860F7" Offset="0.508"/>
</LinearGradientBrush>
</Rectangle.Fill>
<Rectangle.RenderTransform>
<CompositeTransform Rotation="46.097"/>
</Rectangle.RenderTransform>
</Rectangle>
<TextBox x:Name="gameOverText" TextWrapping="Wrap" Text="Game Over" Canvas.Left="428" Canvas.Top="248" Height="107" Width="413" FontSize="72" FontWeight="Bold" FontStyle="Italic"/>
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF1BAE78" Offset="1"/>
<GradientStop Color="#FFDCEE3B"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
有什么想法吗?
答案 0 :(得分:0)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups; //This is used for massage box within a blank applocation.
using Windows.UI.Xaml.Media.Animation;
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
namespace Save_The_Humans
{
/// <summary>
/// A basic page that provides characteristics common to most applications.
/// </summary>
public sealed partial class MainPage : Save_The_Humans.Common.LayoutAwarePage
{
Random random = new Random();
DispatcherTimer enemyTimer = new DispatcherTimer();
DispatcherTimer tagetTimer = new DispatcherTimer();
bool humanCaptured = false;
public MainPage()
{
this.InitializeComponent();
enemyTimer.Tick += enemyTimer_Tick;
enemyTimer.Interval = TimeSpan.FromSeconds(2);
tagetTimer.Tick += tagetTimer_Tick;
tagetTimer.Interval = TimeSpan.FromSeconds(.1);
}
void tagetTimer_Tick(object sender, object e)
{
progressBar.Value += 1;
if (progressBar.Value >= progressBar.Maximum)
EndTheGame();
}
private void EndTheGame()
{
if (!playArea.Children.Contains(gameOverText))
{
enemyTimer.Stop();
tagetTimer.Stop();
humanCaptured = false;
startButton.Visibility = Visibility.Visible;
playArea.Children.Add(gameOverText);
}
}
void enemyTimer_Tick(object sender, object e)
{
AddEnemy();
}
/// <summary>
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
/// </summary>
/// <param name="navigationParameter">The parameter value passed to
/// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
/// </param>
/// <param name="pageState">A dictionary of state preserved by this page during an earlier
/// session. This will be null the first time a page is visited.</param>
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
}
/// <summary>
/// Preserves state associated with this page in case the application is suspended or the
/// page is discarded from the navigation cache. Values must conform to the serialization
/// requirements of <see cref="SuspensionManager.SessionState"/>.
/// </summary>
/// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
protected override void SaveState(Dictionary<String, Object> pageState)
{
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
StartGame();
}
/// <summary>
/// Starts the game.
/// </summary>
public void StartGame()
{
human.IsHitTestVisible = true;
humanCaptured = false;
progressBar.Value = 0;
startButton.Visibility = Visibility.Collapsed;
/*playArea.Children.Add(target);
playArea.Children.Add(human);*/
enemyTimer.Start();
tagetTimer.Start();
}
/// <summary>
/// Starts the animation, below is the enemy method.
/// </summary>
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 properyToAnimate)
{
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, properyToAnimate);
storyboard.Children.Add(animation);
storyboard.Begin();
}
/// <summary>
/// async needs to be a part of the method to show message box!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void About_Click(object sender, RoutedEventArgs e)
{
//Creating instance for the MessageDialog Class
//and passing the message in it's Constructor
MessageDialog msgbox = new MessageDialog("Created by");
//Calling the Show method of MessageDialog class
//which will show the MessageBox
await msgbox.ShowAsync();
}
}
}