无法向按钮单击事件C#UWP添加方法

时间:2016-12-23 08:59:05

标签: c# uwp-xaml

我在使HangMan游戏工作时遇到了一些问题。由于我得到了我的按钮,我不得不制作一个向用户显示字母的方法。所以我有这个Guessing方法我想要添加到按钮点击事件。所以当我尝试向按钮添加方法时,我得到了这个红色的squiqqly线。错误发生在HangMan_OnLoaded方法中。谢谢! HangmangGame Problem

        public void DisplayTheWord()
    {
        WrongGuesses = 0;
        BitmapImage Hangman2 = new BitmapImage();
        Uri URL = new Uri(BaseUri, images[WrongGuesses]);
        Hangman2.UriSource = URL;
        picture.Source = Hangman2;
        string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
        int NextNumber = (new Random().Next(words.Length));
        copyCurrent = "";
        current = words[NextNumber];

        for (int i = 0; i < ReadWords[NextNumber].Length; i++)
        {
            copyCurrent += "_" + "   ";
        }
        CopiedWord.Text = copyCurrent;
    }
    private void Hangman_OnLoaded()
    {
        const int btnSize = 35;
        var c = 0;
        for (var i = 65; i <= 90; i++)
        {
            var btn = new Button();
            btn.Content = (char)i;
            btn.Width = btn.Height = btnSize;
            var margin = btn.Margin;
            margin.Left = c += 37;
            btn.Margin = margin;
            GridMain.Children.Add(btn);
            btn.Click += Guessing();
        }

    }
    private void Guessing(object sender, EventArgs e)
    {
        for (var i = 65; i <= 90; i++)
        {
            var btn = new Button();
            btn = sender as Button;
            btn.Content = (char) i;
            var choice = btn.ToString();
            if (copyCurrent.Contains(choice))
            {
                char[] temp = copyCurrent.ToCharArray();
                char[] find = current.ToCharArray();
                char guessChar = choice.ElementAt(0);
                for (int index = 0; index < find.Length; index++)
                {
                    if (find[index]== guessChar)
                    {
                        temp[index] = guessChar;
                    }
                }
                copyCurrent = new string(temp);
            }
            else
            {
                WrongGuesses++;
            }
            if (WrongGuesses < 6)
            {

            }
        }
    }

    private void DisplayCopy()
    {
        CopiedWord.Text = "";
        for (int index = 0; index < copyCurrent.Length; index++)
        {
            CopiedWord.Text += copyCurrent.Substring(index, 1);
            CopiedWord.Text += " ";
        }
    }

4 个答案:

答案 0 :(得分:1)

您需要从行中删除括号:

btn.Click += Guessing();

这样就变成了:

btn.Click += Guessing;

答案 1 :(得分:0)

我想:

错误是因为猜测是无效的并且不会返回任何东西,但是你会像使用返回事件的方法一样使用猜测:

btn.Click += Guessing();

对于返回值事件的dix:)

答案 2 :(得分:0)

确保您的处理程序适合您要使用的事件:

private void Guessing(object sender, RoutedEventArgs e) {
   //your handler code
}

btn.Click += Guessing;

原因是,每个事件都需要委托签名并定义参数。

答案 3 :(得分:0)

@swatsonpicken说得对。你需要从行中删除括号:

btn.Click += Guessing();

并替换为:

btn.Click += Guessing;

还需要纠正的另一件事是

private void Guessing(object sender, EventArgs e)

写上面的行如下:

private void Guessing(object sender, RoutedEventArgs e)

希望这会有所帮助。 :)