C# 当onlunc方法具有async关键字时更改SplashScreen

C# 当onlunc方法具有async关键字时更改SplashScreen,c#,windows-store-apps,C#,Windows Store Apps,我想在程序更新数据库时更改启动屏幕。在我更改OnLunch事件处理程序之前,一切都很好。基于某些条件,我必须使用async关键字 protected override async void OnLaunched(LaunchActivatedEventArgs args) { bool IsAppUpdated = await CheckDbVersion(); if(IsAppUpdated) { if (args.PreviousExecutio

我想在程序更新数据库时更改启动屏幕。在我更改OnLunch事件处理程序之前,一切都很好。基于某些条件,我必须使用
async
关键字

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
     bool IsAppUpdated = await CheckDbVersion();
     if(IsAppUpdated)
     {
        if (args.PreviousExecutionState != ApplicationExecutionState.Running)
        {
            bool loadState = (args.PreviousExecutionState == ApplicationExecutionState.Terminated);
            SplashScreenExtend extendedSplash = new SplashScreenExtend(args.SplashScreen, loadState);
            Window.Current.Content = extendedSplash;
        }
        bool fine = await ReconstructDatabase();
     }
       //doing sth else

}
问题是当我运行程序时,新的启动屏幕不会出现。但是当我调试代码时,会出现启动屏幕。此外,当我删除async关键字并等待函数时,每个函数都变为ok


请告诉我我的错误在哪里。

好的,这里发生的是这样的:
OnLaunched
事件在启动屏幕有机会加载之前完成,因为它是
async void
。这意味着调用
OnLaunched
的方法将激发,然后不等待响应而继续。在调试中,调用方法通过
OnLaunched
的速度可能会延迟,因为调试器必须加载所有模块的符号,从而使调试器在您有机会看到它之前成功地更改了初始屏幕。不幸的是,您无法将其更改为所需的值,
异步任务
,因为这将a)更改方法的签名,使其不会被重写,b)调用方法可能仍然没有等待它,因此同样的问题也会发生

这对您意味着:您不能在
OnLaunched
中等待
方法。这意味着a)您必须在
SplashScreenExtend
类中执行适当的
等待
或同步运行
CheckDbVersion
重建数据库
方法(除非您可以“设置并忘记”
重建数据库
,在这种情况下,您仍然可以运行它
异步
,但您不能等待它)


希望这有助于您愉快地编码。

您必须在设置windows内容以显示splashscreen后激活当前窗口

SplashScreenExtend extendedSplash = new  SplashScreenExtend(args.SplashScreen, loadState);
Window.Current.Content = extendedSplash;
Window.Current.Activate();

“你不能在OnLaunched中等待方法”…为什么不呢?MSDN有一篇专门讨论这个主题的文章。