如何从 Template10 覆盖 OnLaunched()
How to override OnLaunched() from Template10
我试图覆盖模板 10 Windows 应用程序中的 OnLaunched() 函数,但问题是它被密封在模板 10 BootStrapper class(它继承自应用程序 class).
这是我的方法:
using Windows.UI.Xaml;
...
namespace Sample {
...
sealed partial class App : Template10.Common.BootStrapper {
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
/*************** My stuff *****************
***********************************************/
}
...
}
我正在为此应用程序使用 Template10 空白应用程序,BootStrapper class 中的 OnLaunched() 方法是这样的:
namespace Template10.Common
{
public abstract class BootStrapper : Application
{
...
protected sealed override void OnLaunched(LaunchActivatedEventArgs e);
...
}
...
}
我无法从 BootStrapper 的 OnLaunched() 中删除密封修改器(猜测,因为它是 "from metadata")。
在摘要中包含密封方法有什么意义 class?
我们是否有其他方法来覆盖,如 OnResume()、OnStartAsync() 等,而不是 OnLaunched()?
更新:供参考,这里是 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
}
}
请帮忙:}
您正试图覆盖 MyPage.xaml.cs 中的 OnLaunched,我可以很肯定地假设您的 MyPage class 没有继承自 Application。所以它没有 OnLaunched() 方法(至少没有那个签名)。您需要做的是在 App.xaml.cs 中覆盖它,因为它是 Application.OnLaunched()。 App.xaml.cs 中的 App class 继承自 Application。
顺便说一下,这是您提到的空白应用程序模板中的示例:
模板 10 不允许我们覆盖 OnLaunched() 方法。相反,我们可以为此覆盖 OnInitializeAsync() 和 OnStartAsync() 方法。
原因是Template 10推荐我们使用一种叫做单页模型的东西,也就是使用一个Page实例class放入Framework提供的空Frame中。这对我们有什么好处?好吧,如果我们需要在我们的应用程序中放置一个菜单,比如汉堡包菜单,那么我们需要在我们在应用程序中创建的每个页面中复制菜单代码。这会导致冗余、不一致、WET 代码等问题。
因此,模板 10 最初会创建一个页面,他们称之为 Shell,然后每个页面的内容都会加载到此 Shell 页面中,而不是创建新页面。
我们可以通过以下方式覆盖这些方法:
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);
}
}
这是我找到答案的地方:
https://github.com/Windows-XAML/Template10/wiki/Docs-%7C-HamburgerMenu
所以,长话短说,覆盖 OnInitializeAsync() 或 OnStartAsync(),而不是 OnLaunched()。
我试图覆盖模板 10 Windows 应用程序中的 OnLaunched() 函数,但问题是它被密封在模板 10 BootStrapper class(它继承自应用程序 class).
这是我的方法:
using Windows.UI.Xaml;
...
namespace Sample {
...
sealed partial class App : Template10.Common.BootStrapper {
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
/*************** My stuff *****************
***********************************************/
}
...
}
我正在为此应用程序使用 Template10 空白应用程序,BootStrapper class 中的 OnLaunched() 方法是这样的:
namespace Template10.Common
{
public abstract class BootStrapper : Application
{
...
protected sealed override void OnLaunched(LaunchActivatedEventArgs e);
...
}
...
}
我无法从 BootStrapper 的 OnLaunched() 中删除密封修改器(猜测,因为它是 "from metadata")。
在摘要中包含密封方法有什么意义 class?
我们是否有其他方法来覆盖,如 OnResume()、OnStartAsync() 等,而不是 OnLaunched()?
更新:供参考,这里是 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
}
}
请帮忙:}
您正试图覆盖 MyPage.xaml.cs 中的 OnLaunched,我可以很肯定地假设您的 MyPage class 没有继承自 Application。所以它没有 OnLaunched() 方法(至少没有那个签名)。您需要做的是在 App.xaml.cs 中覆盖它,因为它是 Application.OnLaunched()。 App.xaml.cs 中的 App class 继承自 Application。
顺便说一下,这是您提到的空白应用程序模板中的示例:
模板 10 不允许我们覆盖 OnLaunched() 方法。相反,我们可以为此覆盖 OnInitializeAsync() 和 OnStartAsync() 方法。
原因是Template 10推荐我们使用一种叫做单页模型的东西,也就是使用一个Page实例class放入Framework提供的空Frame中。这对我们有什么好处?好吧,如果我们需要在我们的应用程序中放置一个菜单,比如汉堡包菜单,那么我们需要在我们在应用程序中创建的每个页面中复制菜单代码。这会导致冗余、不一致、WET 代码等问题。
因此,模板 10 最初会创建一个页面,他们称之为 Shell,然后每个页面的内容都会加载到此 Shell 页面中,而不是创建新页面。
我们可以通过以下方式覆盖这些方法:
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);
}
}
这是我找到答案的地方: https://github.com/Windows-XAML/Template10/wiki/Docs-%7C-HamburgerMenu
所以,长话短说,覆盖 OnInitializeAsync() 或 OnStartAsync(),而不是 OnLaunched()。