2013-05-21 2 views
1

J'ai rencontré un problème avec mon Whack a Mole Jeu:Xaml Parse Exception WPF

XamlParseException était non gérée

« L'invocation du constructeur du type 'WhackaMoleReal.MainWindow' qui correspond à la fixation spécifiée les contraintes ont jeté une exception. Numéro de ligne '3' et position de ligne '9'.

Ceci est mon code principal:

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

namespace WhackaMoleReal 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     //Global Variables\\ 
     string[] CmdArgs = Environment.GetCommandLineArgs(); 
     string Moleini; 
     string Root = ""; 
     int ImageSize; 

     public MainWindow() 
     { 
      //Setting .ini Prefences\\ 
      Root = TUtils.GetIniString(Moleini, "Root", "Path", ""); 
      Moleini = CmdArgs[1]; 
      InitializeComponent(); 

      // Dispacher for Full Game Time \\ 
      System.Windows.Threading.DispatcherTimer endGame = new System.Windows.Threading.DispatcherTimer(); 
      endGame.Tick += new EventHandler(endGame_Tick); 
      endGame.Interval = TimeSpan.FromSeconds(5); 
      endGame.Start(); 
     } 

     private void endGame_Tick(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void PopulateGrid() 
     { 
      double NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8); 
      int ImageSize = TUtils.GetIniInt(Moleini, "ImageSize", "imageSize", 50); 
      int ImageBorderSize = TUtils.GetIniInt(Moleini, "ImageBorder", "imageBorder", 2); 
      double NumberOfColumns = TUtils.GetIniInt(Moleini, "NumRowsColumns", "columnNum", 4); 

      // More Columns than Rows \\ 
      if (NumberOfColumns > NumofImages) 
      { 
       MessageBox.Show("There is something wrong with the .ini file."); 
       MainWin.Close(); 
      } 

      // Math - Get Necessary Variables \\ 
      int ColumnSize = (ImageSize + (4 * ImageBorderSize)); 
      int RowSize = (ImageSize + (4 * ImageBorderSize)); 
      int NumberofRows = (int)Math.Ceiling(NumofImages/NumberOfColumns); 
      int MainWindowWidth = (TUtils.ToInt(NumberOfColumns.ToString(), 4) * ColumnSize) + 15; 
      int MainWindowHeight = (NumberofRows * RowSize) + 35; 


      // Set Window Size \\ 
      MainWin.Width = MainWindowWidth; 
      MainWin.Height = MainWindowHeight; 

      // Create Grid \\ 
      Grid grid_Main = new Grid(); 
      MainWin.Content = grid_Main; 
      grid_Main.Height = MainWindowHeight; 
      grid_Main.Width = MainWindowWidth; 

      // Grid Properties \\ 
      for (int i = 0; i < NumberOfColumns; i++) 
      { 
       ColumnDefinition newColumn = new ColumnDefinition(); 
       newColumn.Width = new GridLength(ColumnSize, GridUnitType.Pixel); 
       grid_Main.ColumnDefinitions.Add(newColumn); 
      } 

      for (int i = 0; i < NumberofRows; i++) 
      { 
       RowDefinition Row = new RowDefinition(); 
       Row.Height = new GridLength(RowSize, GridUnitType.Pixel); 
       grid_Main.RowDefinitions.Add(Row); 
      } 

      // Fill Grid \\ 
      int RowCount = 0; 
      int ColumnCount = 0; 
      for (int i = 0; i < NumofImages; i++) 
      { 
       // grid_Main.Children.Add(grid_Main); 

       if (RowCount < NumberofRows) 
       { 
        if (ColumnCount < NumberOfColumns) 
        { 
         Console.WriteLine("ColumnCount: " + ColumnCount.ToString()); 
         Grid.SetRow(CreateImage(), ColumnCount); 
         Grid.SetColumn(CreateImage(), ColumnCount); 
         ColumnCount++; 
        } 

        else 
        { 
         RowCount++; 
         ColumnCount = 0; 
         Grid.SetRow(CreateImage(), ColumnCount); 
         Grid.SetColumn(CreateImage(), ColumnCount); 
         ColumnCount++; 
         Console.WriteLine("RowCount: " + RowCount.ToString()); 
        } 
       } 

       else 
       { 
        break; 
       } 

      } 
     } 

     private Image CreateImage() 
     { 
      // Gets/Sets Necessary Variables \\ 
      double ImageHeight = ImageSize * 0.7; 

      // Initialize Image \\ 
      System.Windows.Controls.Image newImage = new Image(); 

      // Image Properties \\ 
      newImage.Width = ImageSize; 
      newImage.Height = ImageHeight; 

      // Define Name and Content \\ 
      newImage.Name = "Image"; 
      String ImageFunction = TUtils.GetIniString(Moleini, "Image", "PictureFile", Root + "mole2.png"); 
      if (File.Exists(ImageFunction)) 
      { 
       newImage.Source = new BitmapImage(new Uri(ImageFunction)); 
      } 
      else 
      { 
       MessageBox.Show("Cannot find " + ImageFunction + ".", "Please fix the ini file"); 
      } 

      return newImage; 
     } 





    } 

} 

et mon XAML:

<Window x:Name="MainWin" x:Class="WhackaMoleReal.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Image Margin="353,156,-131,-58" Source="mole2.png" Stretch="Fill"/> 
     <TextBlock HorizontalAlignment="Left" Margin="20,10,0,0" TextWrapping="Wrap" Text="Can You Catch the Mole?" VerticalAlignment="Top" Height="79" Width="497" FontFamily="SimHei" FontSize="40"/> 

    </Grid> 
</Window> 
+0

Regardez l'exception interne. –

+2

@HighCore Quand je baisse la flèche vers le bas, il montre un tas de PresentationFramework.dll, et beaucoup d'autres .dll. Si mon patron me laissait le faire dans MVVM, ma vie serait tellement plus facile. – Andrew

+5

Il y a une citation, attribuée à A. Einstein qui va "la définition de la folie fait la même chose encore et encore et s'attendre à des résultats différents." Quittez ce travail et allez ailleurs où vous avez un patron sain. –

Répondre

0

Je trouve mon problème, je viens d'avoir à supprimer et remettre la valeur .ini saisissant .dll :)

0

Environment.GetCommandLineArgs() devrait revenir au moins un tableau de 2 objets, puisque vous faites référence à cela dans votre constructeur

Moleini = CmdArgs[1]; 

J'ai essayé ou t votre code. Environment.GetCommandLineArgs() renvoyait seulement 1 objet.

+0

Alors ... (Je suis désolé je suis très noob), que puis-je faire pour résoudre ce problème? – Andrew

+0

A partir de votre code, il apparaît que Moleini est une valeur constante, puisqu'elle n'est affectée qu'une seule fois. Je dirais hardcode cette valeur pour le moment au lieu de faire Moleini = CmdArgs [1]. – cvraman

+0

Pouvez-vous me donner un exemple, je suis très nouveau à C#, et je suis très confus :( – Andrew