简体   繁体   中英

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).

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:

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").

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()?

Update: For reference, here are all the members in 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. Instead we can override the OnInitializeAsync() and OnStartAsync() methods for this purpose.

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. 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.

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.

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

So, long story short, override OnInitializeAsync() or OnStartAsync(), instead of 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. So it does not have OnLaunched() method (at least not with that signature). What you need to do is override it in App.xaml.cs, as it's Application.OnLaunched(). App class, which is in App.xaml.cs, inherits from Application.

By the way, this is the example from the blank app template, which you've mentioned:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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