问题描述
我正在学习 WP 编码,但遇到了无法解决的问题:/
I'm learning WP coding and I have problem that I can't solve :/
try
{
NavigationService.Navigate(new Uri("/edit.xaml", UriKind.Relative));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error!",MessageBoxButton.OK);
}
edit.xaml 与 MainPage.xaml 位于同一目录
edit.xaml is in the same directory as MainPage.xaml
它抛出NullReferenceException"
It throws "NullReferenceException"
推荐答案
因为你得到一个 NullReferenceException
,我猜你正在尝试调用 NavigationService.Navigate
早期,例如在 MainPage 构造函数中.
Because you get an NullReferenceException
, my guess is that you are trying to call NavigationService.Navigate
to early, for example in the MainPage constructor.
相反,如果您想在加载页面时立即导航,请尝试通过将以下代码添加到 MainPage 类来覆盖 OnNavigatedTo
事件来实现:
Instead, if you want to navigate immedietly when page is loaded for example, try doing it by overriding the OnNavigatedTo
event by adding this code to the MainPage class:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}
这篇关于NavigationService.navigate 空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!