简体   繁体   English

Windows Phone 8.1 NullRefrenceException

[英]Windows Phone 8.1 NullRefrenceException

I'm Currently Working on a windows 8.1 Application and im trying to set the value of a textblock that is inside of a Pivot Page. 我目前正在使用Windows 8.1应用程序,并且正在尝试设置数据透视页内的文本块的值。 When I try to set the value of the text Block I get a weird error about a Null Refrence Exception. 当我尝试设置文本块的值时,出现关于Null Refrence异常的奇怪错误。

The Code for the XAML is as follows XAML的代码如下

<TextBlock x:Name="scoreFinal" Text="0" HorizontalAlignment="Left" Margin="235,408,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" Width="97" FontSize="32"/>

Im using an event Handler for TextChanged in a textbox to change the value of the TextBlock using the following code 我使用以下代码在文本框中为TextChanged使用事件处理程序以更改TextBlock的值

private void score_TextChanged(object sender, TextChangedEventArgs e)
    {
        int totalPar=38;
        int actual=0;


        // actual = int.Parse(score1.Text) + int.Parse(score2.Text) + int.Parse(score3.Text) + int.Parse(score4.Text) + int.Parse(score5.Text) + int.Parse(score6.Text) + int.Parse(score7.Text) + int.Parse(score8.Text) + int.Parse(score9.Text);
        if (actual < totalPar)
        {


            scoreFinal.Text = ("-" + (totalPar - actual));

        }

When I run the page it loads fine and I have values on all my text boxes to be 0 当我运行页面时,它可以很好地加载,并且我所有文本框中的值都为0

but when i run this and edit text I get the following error 但是当我运行它并编辑文本时,出现以下错误

在此处输入图片说明

Anyone got a clue? 有人知道了吗?

There is a chance for scoreFinal control is not initialized when score_TextChanged event fired . 触发score_TextChanged事件时,未初始化scoreFinal控件的机会。

Try to hookup TextChanged in page loaded event . 尝试在页面加载事件中连接TextChanged

   void YourPage_Loaded(object sender, RoutedEventArgs e)
        {
         score2.TextChanged+=score_TextChanged;

        }

Also don't forget to remove the event handler from XAML. 另外,不要忘记从XAML中删除事件处理程序。

In your XAML Code There is no TextChanged Event. 在您的XAML代码中没有TextChanged事件。 I hope it doesn't created Well. 我希望它不会创造嘛。

<TextBlock x:Name="scoreFinal" Text="0" HorizontalAlignment="Left" Margin="235,408,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" Width="97" FontSize="32" TextChanged="score_TextChanged"/>

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

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