OnNavigatedTo 与 Load 事件 [英] OnNavigatedTo vs Load event

查看:16
本文介绍了OnNavigatedTo 与 Load 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几个在线示例中,我发现了这一点:

In several online examples I found this:

public partial class ForecastPage : PhoneApplicationPage
{
    Forecast forecast;

    public ForecastPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // code here
    }
}

但在其他人中,我发现使用了 Load 事件,例如

but in others I found the use of the Load event like

public partial class Person : PhoneApplicationPage
{
  private PersonViewModel _ViewModel;

  public Person()
  {
     InitializeComponent();
     this.Loaded += new RoutedEventHandler(SearchView_Loaded);
  }

  void SearchView_Loaded(object sender, RoutedEventArgs e)
  {
     // code here
  }
}

我知道 OnNavigatedToLoad 事件之前触发,但两者都在 UI 被绘制到手机之前触发,所以我的问题是有什么优势 正在使用另一种方法?

I know that OnNavigatedTo fires before the Load event, but both fire before the UI is drawn into the phone, so my question is Is there any advantage in use one method from the other?

推荐答案

我不同意 Tigran.

I'd disagree with Tigran.

public View()
{
  InitializeComponent();

  personList.ItemsSource = PersonDataSource.CreateList(100);

    Loaded += (sender, args) => Debug.WriteLine("Loaded");
}

  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
      Debug.WriteLine("Navigated");
  }

向前向后跳跃时,输出为

While jumping forward-backward, output is

导航已加载导航已加载导航已加载

Navigated Loaded Navigated Loaded Navigated Loaded

因此,在页面导航完成时调用OnNavigated,但在页面控件加载之前(期间),而Loaded在加载时调用页面已准备就绪,所有控件已加载.

So, OnNavigated is called when page navigation is done, but before(during) page controls are loaded, while Loaded is called when page is ready and all controls are loaded.

这篇关于OnNavigatedTo 与 Load 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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