OnNavigatedTo vs Load event

前端 未结 3 1725
孤独总比滥情好
孤独总比滥情好 2020-12-31 22:20

In several online examples I found this:

public partial class ForecastPage : PhoneApplicationPage
{
    Forecast forecast;

    public ForecastPage()
    {
          


        
3条回答
  •  心在旅途
    2020-12-31 22:47

    In Windows Runtime, the Loaded event will always fire after OnNavigatedTo (even when pages are being cached by setting NavigationCacheMode.Required). Vitalii is right about that.

    According to MSDN:

    In the Windows Runtime implementation, the Loaded event is guaranteed to occur after a control template is applied, and you can obtain references to objects that are created by applying the XAML template.

    For app code that uses navigation between pages, do not use Page.OnNavigatedTo for element manipulation or state change of controls on the destination page. The OnNavigatedTo virtual method is invoked before the template is loaded, thus elements from templates aren't available yet. Instead, attach a Loaded event handler at the root of the newly loaded page's content, and perform any element manipulations, state changes, event wiring and so on in the Loaded event handler.

    But there is a good reason why you would want to use OnNavigatedTo: it is the only place where you can get the navigation parameters. If you never use navigation parameters, use the Loaded event.

提交回复
热议问题