如何在wp8中处理文本块

时间:2014-02-05 06:06:06

标签: c# visual-studio-2012 windows-phone-8

这是我的文本块的代码,

我想在其中显示两个数字的区别,

但它给出了错误,我运行应用程序。

Error: System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.

这是代码,

        TextBlock diffBlock = new TextBlock();


        diffBlock.FontSize = 30;

        diffBlock.Text = " ";

        diffBlock.Text = (total - double.Parse(income.Inc)).ToString();

        ContentPanel.Children.Add(diffBlock);

        Difference.Foreground = new SolidColorBrush(Colors.Black);

        Difference.Text = "Remaining Budget: " + diffBlock;

如何解决?

1 个答案:

答案 0 :(得分:0)

您不能将textbolck用作字符串使用Text属性。这将解决您的问题。

TextBlock diffBlock = new TextBlock();


        diffBlock.FontSize = 30;

        diffBlock.Text = " ";
      if(income!=null)
        diffBlock.Text = (total - double.Parse(income.Inc)).ToString();
       else
         diffBlock.Text = total.ToString();
         ContentPanel.Children.Add(diffBlock);

        Difference.Foreground = new SolidColorBrush(Colors.Black);

        Difference.Text = "Remaining Budget: " + diffBlock.Text;