简体   繁体   English

如何从Template10覆盖OnLaunched()

[英]How to override OnLaunched() from Template10

这是截图

I am trying to override the OnLaunched() function in a Template 10 Windows Application, but the problem is that it is sealed in Template 10 BootStrapper class (which inherits from the Application class). 我试图覆盖Template 10 Windows应用程序中的OnLaunched()函数,但是问题是它被密封在Template 10 BootStrapper类中(该类继承自Application类)。

Here's my method: 这是我的方法:

using Windows.UI.Xaml;
...

namespace Sample {
...
sealed partial class App : Template10.Common.BootStrapper {

protected override void OnLaunched(LaunchActivatedEventArgs args)
{

    /*************** My stuff *****************
    ***********************************************/
}
...
}

I am using Template10 Blank app for this app, and the OnLaunched() method in BootStrapper class is this: 我正在为此应用程序使用Template10 Blank应用程序,BootStrapper类中的OnLaunched()方法是这样的:

namespace Template10.Common
{
public abstract class BootStrapper : Application
{
    ...
    protected sealed override void OnLaunched(LaunchActivatedEventArgs e);
    ...
}
...
}

I cannot remove the sealed modifier from OnLaunched() in BootStrapper (guess because it is "from metadata"). 我无法从BootStrapper的OnLaunched()中删除密封的修饰符(猜测是因为它是“来自元数据”)。

What's the point of including a sealed method in an abstract class? 在抽象类中包含密封方法有什么意义?

Do we get some other method to override, like OnResume(), OnStartAsync(), etc, instead of OnLaunched()? 我们是否还有其他方法可以覆盖,例如OnResume(),OnStartAsync()等,而不是OnLaunched()?

Update: For reference, here are all the members in BootStrapper: 更新:作为参考,以下是BootStrapper中的所有成员:

public abstract class BootStrapper : Application
{
    public const string DefaultTileID = "App";

    protected BootStrapper();

    public static BootStrapper Current { get; }
    public TimeSpan CacheMaxDuration { get; set; }
    public INavigationService NavigationService { get; }
    public StateItems SessionState { get; set; }
    public bool ShowShellBackButton { get; set; }
    protected Func<SplashScreen, UserControl> SplashFactory { get; set; }

    public event EventHandler<WindowCreatedEventArgs> WindowCreated;

    public static AdditionalKinds DetermineStartCause(IActivatedEventArgs args);
    public NavigationService NavigationServiceFactory(BackButton backButton, ExistingContent existingContent);
    [AsyncStateMachine(typeof(<OnInitializeAsync>d__44))]
    public virtual Task OnInitializeAsync(IActivatedEventArgs args);
    public virtual void OnResuming(object s, object e);
    public abstract Task OnStartAsync(StartKind startKind, IActivatedEventArgs args);
    [AsyncStateMachine(typeof(<OnSuspendingAsync>d__45))]
    public virtual Task OnSuspendingAsync(object s, SuspendingEventArgs e);
    public Dictionary<T, Type> PageKeys<T>() where T : struct, IConvertible;
    public virtual T Resolve<T>(Type type);
    public virtual INavigable ResolveForPage(Type page, NavigationService navigationService);
    public void UpdateShellBackButton();
    [AsyncStateMachine(typeof(<OnActivated>d__26))]
    protected sealed override void OnActivated(IActivatedEventArgs e);
    [AsyncStateMachine(typeof(<OnCachedFileUpdaterActivated>d__27))]
    protected sealed override void OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs args);
    [AsyncStateMachine(typeof(<OnFileActivated>d__28))]
    protected sealed override void OnFileActivated(FileActivatedEventArgs args);
    [AsyncStateMachine(typeof(<OnFileOpenPickerActivated>d__29))]
    protected sealed override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args);
    [AsyncStateMachine(typeof(<OnFileSavePickerActivated>d__30))]
    protected sealed override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args);
    protected sealed override void OnLaunched(LaunchActivatedEventArgs e);
    [AsyncStateMachine(typeof(<OnSearchActivated>d__31))]
    protected sealed override void OnSearchActivated(SearchActivatedEventArgs args);
    [AsyncStateMachine(typeof(<OnShareTargetActivated>d__32))]
    protected sealed override void OnShareTargetActivated(ShareTargetActivatedEventArgs args);
    protected sealed override void OnWindowCreated(WindowCreatedEventArgs args);

    public enum AdditionalKinds
    {
        Primary,
        Toast,
        SecondaryTile,
        Other
    }
    public enum BackButton
    {
        Attach,
        Ignore
    }
    public enum ExistingContent
    {
        Include,
        Exclude
    }
    public enum StartKind
    {
        Launch,
        Activate
    }
}

Please help :} 请帮忙 :}

Template 10 does not allow us to override OnLaunched() method. 模板10不允许我们重写OnLaunched()方法。 Instead we can override the OnInitializeAsync() and OnStartAsync() methods for this purpose. 相反,我们可以为此目的重写OnInitializeAsync()和OnStartAsync()方法。

The reason is that Template 10 recommends us to use something called the Single Page Model, which is nothing but using a single instance of the Page class to put in the empty Frame provided by the Framework. 原因是模板10建议我们使用一种称为“单页模型”的东西,它只不过是使用Page类的单个实例放入框架提供的空框架中。 How is that benefit to us? 这对我们有什么好处? Well, if we need to put a menu, say a Hamburger menu, in our app, then we need to copy the code for the menu in each and every page we create in our app. 好吧,如果我们需要在应用程序中放置一个菜单(例如汉堡菜单),则需要在我们在应用程序中创建的每个页面中复制该菜单的代码。 This would lead to things like redundancy, inconsistency, WET code, etc. etc. 这将导致冗余,不一致,WET代码等问题。

Therefore, template 10, initially, creates a Page, which they call the Shell, and then contents of each page is loaded into this Shell page, instead of creating new Pages. 因此,模板10最初会创建一个称为Shell的页面,然后将每个页面的内容加载到此Shell页面中,而不是创建新的Pages。

We can override these methods in the following way: 我们可以通过以下方式覆盖这些方法:

sealed partial class App : BootStrapper
{
public App()
{
    this.InitializeComponent();
}

public override Task OnInitializeAsync(IActivatedEventArgs args)
{
    var nav = NavigationServiceFactory(BackButton.Attach, ExistingContent.Include);
    Window.Current.Content = new Views.Shell(nav);
    return Task.FromResult<object>(null);
}

public override Task OnStartAsync(BootStrapper.StartKind startKind, IActivatedEventArgs args)
{
    NavigationService.Navigate(typeof(Views.MainPage));
    return Task.FromResult<object>(null);
}
}

Here's where I figured the answer: https://github.com/Windows-XAML/Template10/wiki/Docs-%7C-HamburgerMenu 我在这里找到答案的地方: https : //github.com/Windows-XAML/Template10/wiki/Docs-%7C-HamburgerMenu

So, long story short, override OnInitializeAsync() or OnStartAsync(), instead of OnLaunched(). 因此,长话短说,请重写OnInitializeAsync()或OnStartAsync(),而不是OnLaunched()。

You're trying to override OnLaunched in MyPage.xaml.cs and I'm pretty safe to assume that your MyPage class does not inherit from Application. 您试图覆盖MyPage.xaml.cs中的OnLaunched,并且我很安全地假定您的MyPage类不继承自Application。 So it does not have OnLaunched() method (at least not with that signature). 因此它没有OnLaunched()方法(至少没有该签名)。 What you need to do is override it in App.xaml.cs, as it's Application.OnLaunched(). 您需要做的是在App.xaml.cs中覆盖它,因为它是Application.OnLaunched()。 App class, which is in App.xaml.cs, inherits from Application. App.xaml.cs中的App类继承自Application。

By the way, this is the example from the blank app template, which you've mentioned: 顺便说一下,这是您提到的空白应用程序模板中的示例:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM