0

我想在另一页上显示错误消息。我得到了 NullReferenceException,但是查询字符串设置在有错误的页面上。有人会告诉我我的代码有什么问题吗?

 catch (Exception ex)
        {
            //Dispatcher.BeginInvoke(new Action(() =>MessageBox.Show(ex.StackTrace,"Error!",MessageBoxButton.OK)));

           string query=@"/ErrorPage.xaml?msg=" + ex.StackTrace.ToString() ;
           Dispatcher.BeginInvoke(new Action(() =>this.NavigationService.Navigate(new Uri(query, UriKind.Relative))));
        }

当页面加载到其他页面时,有显示错误消息的代码

 public ErrorPage()
    {
        InitializeComponent();
        string msg = NavigationContext.QueryString["msg"].ToString();
        lstMessage.Items.Add(msg);

    }
4

1 回答 1

0

我应该将我的代码放入 MainPage_Load 方法中。有用。

      public ErrorPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);                       
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show(CMSPhoneApp.App.GlobalVariables.strNofifyEmailSubject,
                        "Report Error", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
            //according to the serach  it works on real devices (not on the emulator)
            //the reason the EmailComposer not pop up because can't set up an email account on the emulator
            EmailComposeTask emailcomposer = new EmailComposeTask();
            emailcomposer.To = CMSPhoneApp.App.GlobalVariables.reportAddress;
            emailcomposer.Subject = CMSPhoneApp.App.GlobalVariables.strNofifyEmailSubject;
            emailcomposer.Body = CMSPhoneApp.App.GlobalVariables.errorMsg;
            emailcomposer.Show();

        }
        else
        {
            App.GoBack();
        }
    }
于 2012-12-27T19:05:38.947 回答