Search code examples
c#fileuwpactivation

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).

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. 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.

Here is my 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. Tried this as well without result.


Solution

  • 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. 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.

    For an example check the AssociationLaunching sample on GitHub specifically here - OnFileActivated (which is alternative way to catch file activation).

    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. Finally, it uses Window.Current.Activate() to make sure the window is active.