2013-05-21 72 views
1

我也遇到過跟我打地鼠遊戲的一個問題:XAML分析異常WPF

XamlParseException了未處理

WhackaMoleReal.MainWindow「構造對類型調用'符合指定綁定限制引發了一個例外。'行號「3」和行位置「9」。

這是我的主要代碼:

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





    } 

} 

和我的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

看看內部的異常。 –

+2

@HighCore當我放下箭頭時,它會顯示一堆PresentationFramework.dll和其他很多.dll文件。如果我的老闆會讓我在MVVM中完成,我的生活將會變得更加簡單。 – Andrew

+5

有一句話,歸因於愛因斯坦,他說:「瘋狂的定義一遍又一遍地做着同樣的事情,期待着不同的結果。」離開那份工作,去其他地方,你有一個理智的老闆。 –

回答

0

我發現了我的問題,我只是刪除並放回.ini值攫取.dll :)

0

Environment.GetCommandLineArgs()應該返回至少2個對象的數組,因爲你指的是,在你的構造

Moleini = CmdArgs[1]; 

我試過ou你的代碼。 Environment.GetCommandLineArgs()僅返回1個對象。

+0

所以......(對不起,我很小白),我能做些什麼來解決這個問題? – Andrew

+0

從你的代碼看來,Moleini是一個常數值,因爲它只被賦值一次。我會說現在硬編碼這個值,而不是做Moleini = CmdArgs [1]。 – cvraman

+0

可以舉個例子,我對C#很新,而且我很困惑:( – Andrew