C#中的导航和绑定错误

时间:2015-02-15 07:41:25

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

我在C#中有一个主页面,它从前一页面导航到。所以OnNavigatedTo。在这个OnNavigatedTo我已经启动了一个数组。

     public MainPage()  //default constructor
        {
            this.InitializeComponent();
          this.RandomNumb01 = hello(1, 7);
         }

    protected override void OnNavigatedTo(NavigationEventArgs e)
        {
             FillGraph obj = new FillGraph();   //another class which contains the values 
            graph = obj.enterValue();//My graph will be get values here
        }

   int[,] graph;

public string RandomNumb01_1 { get; set; } //This is for the binding of a button's content in XAML



   string hello(int i,int j)
        {

            string s = "";
            if (graph[i, j] == -1 || graph[i, j] == -10) //I get error here that the array is null
                return s;
            else
            return graph[i, j].ToString();

        }

所以,当我在OnNavigatedTo方法中更新了数组的值时,为什么显示数组为null。另外如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您从构造函数中调用hello。此时,graph尚未创建。 OnNavigatedTo方法直到稍后才会被调用(除了你已经崩溃的事实)。

只需使用调试器逐步执行代码并使用 Autos 窗口,就可以突出显示此问题。