Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override OnFileActivated event in App.cs to make OpenWith works right

I'm working on this Windows Store App, I want it to support Open With when the end user wants to open a file using this app, I added the supported format to the declarations section in the app manifest file as this picture shows :

enter image description here

And I overrode the OnFileActivated event in the App.xaml.cs file as follows :

protected override void OnFileActivated(FileActivatedEventArgs args)
        {
            if (args.Files.Count > 1)
            {
                MessageDialog messageDialog1 = new MessageDialog("DirectTouchPlayer can't open many files, the first file will be opened.");
                messageDialog1.ShowAsync();
            }
            OpenedMediaFile = (StorageFile)args.Files.First();
            MessageDialog messageDialog2 = new MessageDialog(OpenedMediaFile.Path + " " + " : Opened !");
            messageDialog2.ShowAsync();
        }

This event seems to not be firing when I perform OpenWith on a .MP4 file, the application lunches but can't quit the splash screen, I tried to debug the event like this thread explains but the debugger doesn't stop at any breakpoint marked in the OnFileActivated method, I appreciate your help.

Update 1 : When OpenWith on a file , and after that the app can't quite the Splash Screen , I'm having now this message box error

enter image description here

Update 2:

I changed my OnFileActivated event handler to be like this :

protected override async void OnFileActivated(FileActivatedEventArgs args)
        {
            var loadMediaFileTask = new Task<IStorageItem>(() =>
                                                          {
                                                              return args.Files.First();
                                                          });
            OpenedFile = await loadMediaFileTask;
        }

where OpenedFile is a :

public static IStorageItem OpenedFile { get; set; }

That I pass as a parameter through the navigation in the OnLaunched event:

if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(PlayerPageView), OpenedFile);
            }

And PlayerPageView is the page that will consume the video in a MediaElement

All this update also doesn't work , I don't understand what I should do, I would be happy if you clarify things to me.

like image 666
AymenDaoudi Avatar asked Jan 15 '14 16:01

AymenDaoudi


1 Answers

OnLaunched is likely not getting called, as that is the method that gets called when the app is opened normally by the user. You need to do most of the same logic that you likely have inside of OnLaunched inside of the OnFileActivated method.

The sample that they provide has this note about OnLaunched:

/// <summary> 
/// Invoked when the application is launched normally by the end user.  Other entry points 
/// will be used when the application is launched to open a specific file, to display 
/// search results, and so forth. 
/// </summary> 
/// <param name="args">Details about the launch request and process.</param> 

Here is the sample version that Microsoft provides in their Windows 8.1 App Sample for Launching by Association:

/// <summary> 
// Handle file activations. 
/// </summary> 
protected override async void OnFileActivated(FileActivatedEventArgs args) 
{ 
    Frame rootFrame = Window.Current.Content as Frame; 
    // Do not repeat app initialization when the Window already has content, 
    // just ensure that the window is active 
    if (rootFrame == null) 
    { 
        // Create a Frame to act as the navigation context and navigate to the first page 
        rootFrame = new Frame(); 
        // Associate the frame with a SuspensionManager key 
        SuspensionManager.RegisterFrame(rootFrame, "AppFrame"); 
        if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) 
        { 
            // Restore the saved session state only when appropriate 
            try 
            { 
                await SuspensionManager.RestoreAsync(); 
            } 
            catch (SuspensionManagerException) 
            { 
                //Something went wrong restoring state. 
                //Assume there is no state and continue 
            } 
        } 

        // Place the frame in the current Window 
        Window.Current.Content = rootFrame; 
    } 

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

    var p = rootFrame.Content as MainPage; 
    p.FileEvent = args; 
    p.ProtocolEvent = null; 
    p.NavigateToFilePage(); 

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

I suggest emulating this as much as possible.

One other thing to note is that I don't suggest popping up MessageDialogs in the Activated event at all. Save that for the page that you arrive at (even an ExtendedSplash screen), when you're sure that the UI thread is free.

Hope this helps and happy coding!

like image 75
Nate Diamond Avatar answered Sep 23 '22 15:09

Nate Diamond