简体   繁体   中英

Xaml Parse Exception WPF

I have encountered a problem with my Whack a Mole Game:

XamlParseException was unhandled

'The invocation of the constructor on type 'WhackaMoleReal.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

This is my main code:

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





    }

}

and my 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>

Environment.GetCommandLineArgs() should return at least an array of 2 objects, since you are referring to that in your constructor

Moleini = CmdArgs[1];

I tried out your code. Environment.GetCommandLineArgs() was returning only 1 object.

我发现了我的问题,我只需要删除并放回.ini值获取.dll :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM