简体   繁体   English

激活文件后,UWP应用在初始屏幕上冻结

[英]UWP app frozen on splash screen when file activated

I have an interesting problem, hard to debug since it happens when I use file activation to launch this app. 我有一个有趣的问题,很难调试,因为当我使用文件激活来启动此应用程序时会发生。 If I launch the app directly, no problem. 如果我直接启动应用程序,没问题。 If I double-click on the associated file, it hangs at splash screen, not even going past this (setup a debug Breakpoint at InitializeComponent , it's not even getting there). 如果我双击关联的文件,它会挂在初始屏幕上,甚至不会经过此屏幕(在InitializeComponent设置调试Breakpoint,甚至没有到达该位置)。

So what I did is: in the Manifest's Declaration tab, I added a File Type Association to the file type I created and ensured "Open is safe" was checked. 所以我要做的是:在清单的“声明”选项卡中,我向创建的文件类型添加了文件类型关联,并确保选中了“打开是安全的”。 Then, used OnNavigatedTo override to catch the file name of the file that was used to activate. 然后,使用OnNavigatedTo覆盖以捕获用于激活的文件的文件名。 I get the splash screen and then nothing. 我看到启动画面,然后什么也没有。

If I just launch the application and open the file from within, everything works. 如果我只是启动应用程序并从内部打开文件,则一切正常。 What beats me is that I'm using the exact OnNavigatedTo in another app and it works flawlessly. 令我感到OnNavigatedTo是,我在另一个应用程序中使用的是精确的OnNavigatedTo ,它可以完美运行。

Here is my OnNavigatedTo: 这是我的OnNavigatedTo:

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        var args = e.Parameter as Windows.ApplicationModel.Activation.IActivatedEventArgs;
        if (args != null)
        {
            if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.File)
            {
                var fileArgs = args as Windows.ApplicationModel.Activation.FileActivatedEventArgs;
                string strFilePath = fileArgs.Files[0].Path;
                var file = (StorageFile)fileArgs.Files[0];
                //MainPlayList is a custom object used to manipulate the playlist of stuff I'm building.
                MainPlayList = new Playlist();
                MainPlayList.InitializePlayList();
                await MainPlayList.AddImageToPlaylist(file);
             }
       }
    }

I've checked the package manifest for the working application and this one. 我已经检查了正在运行的应用程序和此应用程序的软件包清单。 Aside from the name, they are identical. 除了名称,它们是相同的。 Someone mentioned about 18 months ago that it may be related to the Windows version used as minimum. 大约18个月前,有人提到它可能与Windows版本最低有关。 Tried this as well without result. 也尝试了此,但没有结果。

This means the app never leaves the activation handler in App.xaml.cs - when the app is not launched yet, and is file activated, it never goes to OnLauched and calls OnActivated method , which you can override - and you should - to initialize the root Frame and activate the Window . 这意味着该应用程序永远不会在App.xaml.csApp.xaml.cs激活处理程序-当该应用程序尚未启动并被文件激活时,它永远不会进入OnLauched并调用OnActivated方法 ,您可以覆盖该方法 ,并且应该-进行初始化根Frame并激活Window Essentially you need to perform the same steps as in OnLaunched - so usually you can just turn the initialization into a method and call it from both OnLaunched and OnActivated . 本质上,您需要执行与OnLaunched相同的步骤-因此通常您可以将初始化转换为方法,然后从OnLaunchedOnActivated调用它。

For an example check the AssociationLaunching sample on GitHub specifically here - OnFileActivated (which is alternative way to catch file activation). 有关示例,请在此处专门查看 GitHub上的AssociationLaunching示例 OnFileActivated (这是捕获文件激活的另一种方法)。

protected override void OnFileActivated(FileActivatedEventArgs e)
{
    Frame rootFrame = CreateRootFrame();

    if (rootFrame.Content == null)
    {
        if (!rootFrame.Navigate(typeof(MainPage)))
        {
            throw new Exception("Failed to create initial page");
        }
    }

    var p = rootFrame.Content as MainPage;
    p.NavigateToPageWithParameter(2, e);

    // Ensure the current window is active
    Window.Current.Activate();
}

In case app wasn't started before, the CreateRootFrame first creates and sets the root frame so that it is ready. 如果之前没有启动应用程序,则CreateRootFrame首先创建并设置根框架,使其准备就绪。 Finally, it uses Window.Current.Activate() to make sure the window is active. 最后,它使用Window.Current.Activate()确保该窗口处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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