2017-02-11 11 views
0

Я новичок в C#, и я пытаюсь создать базовую игру (из книги), однако я нахожусь в точке, где она больше не будет работать из-за следующей ошибкиC# - информация WinRT: элемент уже является дочерним элементом другого элемента. Дополнительная информация: Установленные компоненты не обнаружены

исключение типа «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(); 
    } 

Глядя на схему документа, это также похоже в правильном порядке; Document Outline
Единственное, что мне кажется, это 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

Непонятно, почему вы вызываете playArea.Children.Add(), когда эти элементы уже являются дочерними элементами playArea. Как показано как xaml, так и контуром. Наверное, о чем он жалуется. Поместите большую стрелку на утверждение, которое выдает это исключение. И прокомментируйте эти два звонка, если моя догадка правильная, тогда она решает проблему. –

+0

Спасибо, что сделал работу, я не на 100%, чтобы быть честным. Это моя первая реальная попытка на C#, и я следую за книгой (Head First), вероятно, где я ошибаюсь. – Tom

+0

Я добавлю свой код ниже, чтобы это имело смысл: – Tom

ответ

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(); 
     } 
    } 
}