Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Template10重写OnLaunched()_C#_Windows_Asynchronous_Async Await_Template10 - Fatal编程技术网

C# 如何从Template10重写OnLaunched()

C# 如何从Template10重写OnLaunched(),c#,windows,asynchronous,async-await,template10,C#,Windows,Asynchronous,Async Await,Template10,我试图重写Template 10 Windows应用程序中的OnLaunched()函数,但问题是它被密封在Template 10引导程序类(从应用程序类继承)中 以下是我的方法: using Windows.UI.Xaml; ... namespace Sample { ... sealed partial class App : Template10.Common.BootStrapper { protected override void OnLaunched(LaunchActiva

我试图重写Template 10 Windows应用程序中的OnLaunched()函数,但问题是它被密封在Template 10引导程序类(从应用程序类继承)中

以下是我的方法:

using Windows.UI.Xaml;
...

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

protected override void OnLaunched(LaunchActivatedEventArgs args)
{

    /*************** My stuff *****************
    ***********************************************/
}
...
}
我正在为此应用程序使用Template10 Blank应用程序,BootStrapper类中的OnLaunched()方法如下:

namespace Template10.Common
{
public abstract class BootStrapper : Application
{
    ...
    protected sealed override void OnLaunched(LaunchActivatedEventArgs e);
    ...
}
...
}
我无法从BootStrapper中的OnLaunched()中删除密封修饰符(猜测是因为它是“来自元数据”)

在抽象类中包含密封方法有什么意义

我们是否需要重写其他方法,比如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
    }
}
公共抽象类引导程序:应用程序
{
public const string DefaultTileID=“App”;
受保护的引导程序();
公共静态引导程序当前{get;}
公共TimeSpan CacheMaxDuration{get;set;}
公共INavigationService导航服务{get;}
public StateItems会话状态{get;set;}
公共bool ShowShellBackButton{get;set;}
受保护的函数{get;set;}
创建公共事件事件处理程序窗口;
公共静态附加类型确定原因(IActivatedEventArgs args);
公共导航服务导航服务工厂(BackButton BackButton,现有内容ExistingContent);
[AsyncStateMachine(typeof(d_u44))]
公共虚拟任务OnInitializeAsync(IActivatedEventArgs args);
恢复时的公共虚拟无效(对象s、对象e);
公共抽象任务OnStartAsync(StartKind StartKind,IActivatedEventArgs args);
[AsyncStateMachine(typeof(d_u45))]
SuspendingAsync上的公共虚拟任务(对象s、SuspendingEventArgs e);
公共字典PageKeys(),其中T:struct,IConvertible;
公共虚拟T解析(类型);
公共虚拟不可用ResolveForPage(类型页,导航服务导航服务);
public void UpdateShellBackButton();
[AsyncStateMachine(typeof(d_u26))]
受保护的密封超驰无效激活(IActivatedEventArgs e);
[AsyncStateMachine(typeof(d_u27))]
cachedFileUpdateActivatedEventArgs激活时受保护的密封覆盖无效(cachedFileUpdateActivatedEventArgs);
[AsyncStateMachine(typeof(d_uu28))]
已激活受保护的密封覆盖无效(FileActivatedEventArgs args);
[AsyncStateMachine(typeof(d_u29))]
已激活受保护的密封覆盖无效OnFileOpenPickerActivatedEventArgs(文件OpenPickerActivatedEventArgs);
[AsyncStateMachine(typeof(d_u30))]
已激活文件SavePickerActivatedEventArgs(文件SavePickerActivatedEventArgs)上的受保护密封覆盖无效;
仅启动受保护的密封超驰无效(启动ActivatedEventArgs e);
[AsyncStateMachine(typeof(d_u31))]
search激活时受保护的密封覆盖无效(SearchActivatedEventArgs args);
[AsyncStateMachine(typeof(d_uu32))]
ShareTargetActivatedEventArgs上受保护的密封覆盖无效(ShareTargetActivatedEventArgs args);
WindowCreated上受保护的密封覆盖无效(WindowCreatedEventArgs args);
公共枚举附加类型
{
主要的,重要的
干杯
二次瓷砖,
另外
}
公共枚举BackButton
{
贴上
忽视
}
公共枚举现有内容
{
包括
排除
}
公共枚举起始类
{
发射
使活动
}
}

请帮助:}

您试图覆盖MyPage.xaml.cs中的OnLaunched,我可以很安全地假设您的MyPage类不从应用程序继承。因此它没有OnLaunched()方法(至少没有该签名)。您需要做的是在App.xaml.cs中重写它,因为它是Application.OnLaunched()。App类(位于App.xaml.cs中)从应用程序继承

顺便说一下,这是空白应用程序模板的例子,你已经提到过:


您试图覆盖MyPage.xaml.cs中的OnLaunched,我可以很安全地假设您的MyPage类不会从应用程序继承。因此它没有OnLaunched()方法(至少没有该签名)。您需要做的是在App.xaml.cs中重写它,因为它是Application.OnLaunched()。App类(位于App.xaml.cs中)从应用程序继承

顺便说一下,这是空白应用程序模板的例子,你已经提到过:


模板10不允许我们重写OnLaunched()方法。相反,我们可以为此重写OnInitializeAsync()和OnStartAsync()方法

原因是模板10建议我们使用称为单页模型的东西,它只不过是使用页面类的单个实例来放入框架提供的空框架中。这对我们有什么好处?嗯,如果我们需要在我们的应用程序中放置菜单,比如汉堡菜单,那么我们需要在应用程序中创建的每个页面中复制菜单的代码。这将导致冗余、不一致、湿代码等问题

因此,模板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);
}
}
密封部分类应用程序:引导程序
{
公共应用程序()
{
this.InitializeComponent();
}
公共覆盖任务OnInitializeAsync(IActivatedEventArgs args)
{
var nav=导航服务工厂(BackButton.Attach,ExistingContent.Include);
Window.Current.Content=新视图.Shell(nav);
返回Task.FromResult(空);
}
公共覆盖任务OnStartAsync(BootStrapper.StartKind StartKind,IActivatedEventArgs args)
{
NavigationService.Navigate(typeof(Views.MainPage));
返回Task.FromResult(空);
}
}
我在这里找到了答案:

因此,长话短说,重写OnInitializeAsync()或OnStartAsync(),而不是OnLaunched()