locked
File activation problem

    Question

  • Hello,

    I tried to chandle file activation with the help of msdn example(http://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec), but I can't make it working. Here's my App.xaml.cs code:

    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();
            }

    and here's my MainPage.xaml.cs code(only important parts in this case):

    private FileActivatedEventArgs _fileEventArgs/* = null*/;
            public FileActivatedEventArgs FileEvent
            {
                get { return _fileEventArgs; }
                set { _fileEventArgs = value; }
            }
            
    
            private ProtocolActivatedEventArgs _protocolEventArgs = null;
            public ProtocolActivatedEventArgs ProtocolEvent
            {
                get { return _protocolEventArgs; }
                set { _protocolEventArgs = value; }
            }
    
    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
               //navigationHelper.OnNavigatedTo(e);
                if (FileEvent == null)
                    pageTitle.Text = "qwertyuiop";           
                                       
            }
    What do I do wrong? pageTitle.Text always changes, because FileEvent is always null, even after opening the app by the "open with" option.

    Monday, March 03, 2014 9:28 PM

Answers

  • no that isnt posible because then the page isnt loaded. implement a new method like:

    public void NavigateToFilePage()
    {
     if (FileEvent == null)
                    pageTitle.Text = "qwertyuiop";           
    }
    remove the code from the onnavigatedto

    and uncomment this line in the OnFileLaunched:         //p.NavigateToFilePage();


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by lewan0996 Tuesday, March 04, 2014 9:15 PM
    Tuesday, March 04, 2014 9:11 PM

All replies

  • Your code looks superficially correct.

    Where does the problem occur? If you debug the OnFileActivated() method do you get the launched file in the FileActivatedEventArgs? Does it get set correctly onto the page's FileEvents property? etc.?

    Can you reproduce the problem with the sample or only with your modified app?

    If you need us to look deeper can you provide a minimal repro sample, exact repro steps, and a clear description of the desired behavior and how it differs from the actual behavior?

    --Rob

    Monday, March 03, 2014 11:37 PM
    Owner
  • Hello,

    p.FileEvent = args;

    That line doesn't work. I can't send anything to my MainPage. FileEvent is always null. I tried to send other variables to the MainPage, but I can't do it even with simple int variable. When I try to open the app by file activation, it opens, but doesn't receive any file.

    Tuesday, March 04, 2014 8:33 PM
  • you have problem with the order.

    you first navigate to the mainpage (OnNavigateTo) is called. then you set the FileEvent in the OnFileActivated method. you need to add a method to handle the FileEvent after you set it


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, March 04, 2014 8:57 PM
  • Do I have to place p.FileEvent = args; before navigating to the page? I don't know if I understand well, because that doesn't help.
    Tuesday, March 04, 2014 9:08 PM
  • no that isnt posible because then the page isnt loaded. implement a new method like:

    public void NavigateToFilePage()
    {
     if (FileEvent == null)
                    pageTitle.Text = "qwertyuiop";           
    }
    remove the code from the onnavigatedto

    and uncomment this line in the OnFileLaunched:         //p.NavigateToFilePage();


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by lewan0996 Tuesday, March 04, 2014 9:15 PM
    Tuesday, March 04, 2014 9:11 PM
  • That is the solution. Thank you very much.
    Tuesday, March 04, 2014 9:16 PM