NavigationService.Navigate 中的对象引用错误 [英] Object reference error in NavigationService.Navigate

查看:31
本文介绍了NavigationService.Navigate 中的对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 MainPage 构造函数中,我使用 NavigationService 导航到另一个页面,但出现错误:

Inside my MainPage constructor I'm using NavigationService to navigate to another page, but I'm getting the error:

未将对象引用设置为对象的实例

object reference not set to an instance of an object

有谁知道为什么我无法从构造函数导航?

Does anyone know why I'm not able to navigate from my constructor?

public MainPage()
    {           
      InitializeComponent();
      NavigationService.Navigate(new Uri("/test.xaml", UriKind.Relative));
    }

推荐答案

您要导航到的页面尚未在 c'tor 中创建.已加载"在对象全部创建后触发,是执行导航到另一个页面等操作的正确位置.

The page you're navigating to hasn't yet been created in the c'tor. "Loaded" fires after the objects are all created, and is the right spot for doing things like navigating to another page and such.

这篇关于NavigationService.Navigate 中的对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆