private static async Task <Tuple <ExtendedSplashScreenContent, TaskCompletionSource <object> > > CreateExtendedSplashScreenContentAsync(Window hostWindow, AppStartInfo info) { // 创建创建扩展启动屏幕方法为 null。 if (info.ExtendedSplashScreen == null) { return(Tuple.Create <ExtendedSplashScreenContent, TaskCompletionSource <object> >(null, null)); } ExtendedSplashScreenContent extendedSplashScreenContent = null; TaskCompletionSource <object> extendedSplashScreenTcs = null; await hostWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { extendedSplashScreenContent = info.ExtendedSplashScreen(); if (extendedSplashScreenContent != null) { extendedSplashScreenTcs = new TaskCompletionSource <object>(); EventHandler extendedSplashScreenFinishedHandler = null; extendedSplashScreenFinishedHandler = delegate { extendedSplashScreenContent.Finished -= extendedSplashScreenFinishedHandler; extendedSplashScreenTcs.SetResult(null); }; extendedSplashScreenContent.Finished += extendedSplashScreenFinishedHandler; } }); return(Tuple.Create <ExtendedSplashScreenContent, TaskCompletionSource <object> >(extendedSplashScreenContent, extendedSplashScreenTcs)); }
private static async Task ShowExtendedSplashScreenAsync(Window hostWindow, bool isNewWindow, IActivatedEventArgs args, AppStartInfo info) { // 窗口中是否已经有内容。 if (await GetIsWindowExistContentAsync(hostWindow)) { return; } var temp = await CreateExtendedSplashScreenContentAsync(hostWindow, info); // 扩展屏幕内容。 ExtendedSplashScreenContent extendedSplashScreenContent = temp.Item1; // 扩展启动屏幕结束回调信号。 TaskCompletionSource <object> extendedSplashScreenTcs = temp.Item2; // 窗口内容。 UIElement hostWindowContent = null; if (extendedSplashScreenContent != null) { // 如果扩展屏幕窗口内容不为 null,则创建扩展启动屏幕容器并赋值到 hostWindowContent 变量。 hostWindowContent = await BuildExtendedSplashScreenAsync(hostWindow, extendedSplashScreenContent, args); } if (hostWindowContent == null && isNewWindow) { // 对于新窗口,必须确保存在内容。 await hostWindowContent.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { hostWindowContent = new ContentControl(); }); } await ShowWindowContentAsync(hostWindow, hostWindowContent); if (isNewWindow) { // 切换到新窗口。 await SwitchToWindowAsync(hostWindow); } // 使用了扩展启动屏幕。 if (extendedSplashScreenTcs != null) { // 等待回调信号。 await extendedSplashScreenTcs.Task; } // 清除窗口内容,准备导航操作。 await hostWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { SetContent(hostWindow, null); }); }
private static async Task <UIElement> BuildExtendedSplashScreenAsync(Window hostWindow, ExtendedSplashScreenContent extendedSplashScreenContent, IActivatedEventArgs args) { // 用于等待 ExtendedSplashScreen 构造完成。因为 RunAsync 方法第二个参数签名是 async void,不是 async Task。 TaskCompletionSource <UIElement> containerTcs = new TaskCompletionSource <UIElement>(); await hostWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { ExtendedSplashScreen extendedSplashScreen = await ExtendedSplashScreen.CreateAsync(args.SplashScreen, extendedSplashScreenContent); containerTcs.SetResult(extendedSplashScreen); }); return(await containerTcs.Task); }