“base.OnNavigatedTo(e)"的目的OnNavigatedTo 覆盖方法的内部? [英] Purpose of "base.OnNavigatedTo(e)" inside of the OnNavigatedTo override method?

查看:36
本文介绍了“base.OnNavigatedTo(e)"的目的OnNavigatedTo 覆盖方法的内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当覆盖页面中的 OnNavigatedTo 方法时,他们将这行代码放在里面:

When overriding the OnNavigatedTo method in a page they place this line of code inside:

base.OnNavigatedTo(e);

我一直在删除它并且没有注意到任何奇怪的行为.这行代码是干什么用的?我们应该离开它吗?或者这就像一个占位符?

I've been removing it and have not noticed any odd behavior. What is this line of code for? Are we supposed to leave it? Or is this like a place holder?

我很确定这不是特定于方法本身,因为我在不同的地方看到过.我的猜测是,这从我们继承的类(在本例中为 Page)调用了默认的 OnNavigatedTo 方法.它的存在并没有真正的意义,因为如果我们想要它,为什么首先要覆盖它?有人可以解释一下这是如何工作的吗?

I'm pretty sure this isn't specific to the method itself, as I have seen this in different places. My guess is that this calls the default OnNavigatedTo method from the from the class that we are inheriting from (in this case Page). Its existence doesn't really make sense though, because if we wanted that why override it in the first place? Can someone explain how this works?

推荐答案

它不像 Android 那样挑剔(它会因 SuperNotCalledException 而崩溃).但这里有一个离开它的用例:

It isn't as picky as Android is (which crashes with SuperNotCalledException). But here's a use case for leaving it:

  public class BasePage : PhoneApplicationPage
    {
       protected override OnNavigatedTo(....)
    {
        //some logic that should happen on all your pages (logging to console, etc.)
    }
    }

    public class DetailsPage : BasePage
    {
      protected override OnNavigatedTo(....)
    {
    base.OnNavigatedTo(); //the basepage logging, etc.
        //custom page logic (setup VM, querystring parameters, etc.)
    }

}

一般来说,我会称之为.如果 PhoneApplicationPage 的实现发生了变化,并且 Virtual 函数中有更多内容,那么您不想错过 ;)

In general, i'd call it. If the implementation of PhoneApplicationPage changes, and that Virtual function has more in it, you don't want to miss out ;)

这篇关于“base.OnNavigatedTo(e)"的目的OnNavigatedTo 覆盖方法的内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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