简体   繁体   English

更改按钮文本-Windows Phone 7

[英]Change Button Text - Windows Phone 7

How do you change the text of a button in Windows Phone 7 and C#? 如何在Windows Phone 7和C#中更改按钮的文本? NullPointer Exception too, if I am doing the changing of the button text right, what is the problem? NullPointer异常也是如此,如果我在正确更改按钮文本,那是什么问题?

public void CreateWords(Word[] theWords)
{
    listOfWords = new Button[theWords.Length]; //Button Array
    textBlock1.Text = theWords[0].getWord(); //Totally Works
    for (int i = 0; i < theWords.Length; i++)
    {
        listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception
    }

    for (int i = 0; i < theWords.Length; i++)
    {
        stackWords.Children.Add(listOfWords[i]);
    }
}

之所以得到NullReferenceException是因为在创建新的Button数组时,尚未初始化该数组的任何元素(每个元素仍为null)。

As Justin said earlier, you have just created an Array of the Type of Button, You have not added any Button's to the Array as of yet. 正如Justin先前所说,您刚刚创建了一个按钮类型的数组,到目前为止您还没有向该数组中添加任何按钮。 You need to explicitly set each index of the Array to a Button: Try doing something like this. 您需要将Array的每个索引显式设置为Button:尝试执行以下操作。

for (int i = 0; i < theWords.Length; i++) 
{ 
    listOfWords[i] = new Button();
    listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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