locked
LaunchUriAsync only work on debug mode

    Question

  • Hi, 

    I am trying to launch are from my application, but it only works when I am running the application in debug mode. 

    When the application is set up in the metro bar, and I am activating it from there (by tapping on it), it does nothing. 

    I also tried to launch simple URI like http and not necessarily custom URI (another application that I created). 

    I searched for a solution and I found something, but it uses Javascript instead of C sharp. 

    Any ideas? 

    Thanks in advance!

    Sunday, March 23, 2014 3:19 PM

Answers

  • Hi,

    Try putting in a "GotFocus" event when your Page UI loads and put your Launch logic there. For example, use something like the below code. The key is to make sure that your app/page is visible to the user before trying to execute LaunchUriAsync. When it works in debug mode, the debugger is slowing your application enough to make the app visible before the launch occurs.

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.GotFocus += MainPage_GotFocus;
            }
    
            void MainPage_GotFocus(object sender, RoutedEventArgs e)
            {
                this.GotFocus -= MainPage_GotFocus;
                string uri = "http://bing.com";
                Launch(uri);
            }
            
            private async Task<bool> Launch(string uri)
            {
                var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));
    
                if (success)
                {
                    return true;
    
                }
                else
                {
                    return false;
                }
            }
        }



    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Wednesday, March 26, 2014 5:46 PM
    Moderator
  • Hi, 

    The issue was escalated To Microsoft. The solution isn't suitable for my needs, yet we found the issue. 

    When using GetFocus event, you must have some element that request the focus. I haven't had such an element in my code, so I added a button. 

    Thanks to you all for your help! 


    ynahmany

    Sunday, March 30, 2014 6:52 AM

All replies

  • Hi ynahmany,

    Can I have a look at your code or have a simple demo for me to reproduce.

    However I create a demo that only contains LaunchUriAsync function and test with Release mode, works fine.

    I'm not sure if there is some other code in your app block the function?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, March 24, 2014 1:42 AM
    Moderator
  • Sure and thanks for your reply, 

    Here is a sample of my code: 

    bool test = await Launch("http://www.bing.com");

    private async Task<bool> Launch(string uri)
            {                                                         
                var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));

                if (success)
                {
                    return true;

                }
                else
                {                             
                    return false;
                }
            }

    I wrote some logs during the code to a file and i can see that the parameter is being passed to the method and  the URI object is properly created. 

    This method its invocation is located in App.xaml.cs since this application should do one thing only: call another application(using URI scheme)- in this example I want to launch the browser. 

    Thanks in advance! 

    Monday, March 24, 2014 6:25 AM
  • Hi Ynahmany,

    "This method its invocation is located in App.xaml.cs since this application should do one thing only: call another application(using URI scheme)- in this example I want to launch the browser. "

    Okey, but you in which part of method you put the code? If you put the code in OnLaunched(), definitely a false would return because system is not ready, you have to ensure the system is ready for launch another app.

    However, if you put the code in OnSuspending(), should be ok, because your system is kind of ready at that time.

            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                //TODO: Save application state and stop any background activity
                bool test = await Launch("http://www.bing.com");
                deferral.Complete();
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, March 24, 2014 10:03 AM
    Moderator
  • Hi, 

    OnSuspendiung doesn't work either. Moreover, in case i will invoke the method from onSuspending it will not work as I would like. 

    As for the problem you stated about " system is not ready"- i placed, the method in my Main Page and also from there the method doesn't succeed. 

    Any ideas? 

    Thanks for your help so far!

    Monday, March 24, 2014 11:10 AM
  • what kind of capability do you have turned on?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, March 24, 2014 11:45 AM
  • Internet (client) and Private Networks (Client & Server). 

    What really bugs me its the fact that it works in debug mode but not when I am launching the application from the Metro. 

    Monday, March 24, 2014 12:57 PM
  • If you put a button on the page, and then make the call from the button's click handler, does it work there?

    Darin R.

    Monday, March 24, 2014 4:12 PM
  • Hi ynahmany,

    The code I wrote actually works fine with my environment(That's also why I paste here :)).  

    For this reason I believe the problem might not be on your code, should be on your environment.

    Could you give a try if you create a blank project and put in your code, test with release mode, will the same thing happen?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Tuesday, March 25, 2014 1:25 AM
    Moderator
  • I tried the button suggestion and it does work. 

    How can I now transfer this solution to a solution which the user shouldn't press any button? 

    Thanks! 

    Tuesday, March 25, 2014 7:12 AM
  • Hi Jamles,

    Unfortunately, it doesn't work for me. I tried a new application (blank), both in VS12 and VS13. Moreover, I tried on different machines- Win 8 and Win 8.1. Still nothing. 

    I added a button to my code and it does work in that way (Darin suggested it). 

    Tuesday, March 25, 2014 7:15 AM
  • Hi, 

    Is there any update on this  topic? 

    Thanks in advance!


    ynahmany

    Wednesday, March 26, 2014 12:40 PM
  • Hi,

    Try putting in a "GotFocus" event when your Page UI loads and put your Launch logic there. For example, use something like the below code. The key is to make sure that your app/page is visible to the user before trying to execute LaunchUriAsync. When it works in debug mode, the debugger is slowing your application enough to make the app visible before the launch occurs.

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.GotFocus += MainPage_GotFocus;
            }
    
            void MainPage_GotFocus(object sender, RoutedEventArgs e)
            {
                this.GotFocus -= MainPage_GotFocus;
                string uri = "http://bing.com";
                Launch(uri);
            }
            
            private async Task<bool> Launch(string uri)
            {
                var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));
    
                if (success)
                {
                    return true;
    
                }
                else
                {
                    return false;
                }
            }
        }



    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Wednesday, March 26, 2014 5:46 PM
    Moderator
  • Thanks for your help with the Get Focus, but it doesn't work either. 

    All of these solutions are good, but not suitable for my application. 



    ynahmany

    Thursday, March 27, 2014 7:42 AM
  • Can you provide a working solution so we can see if it works on our machine? You may be running into an environmental problem.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, March 27, 2014 12:23 PM
    Moderator
  • Hi, 

    The issue was escalated To Microsoft. The solution isn't suitable for my needs, yet we found the issue. 

    When using GetFocus event, you must have some element that request the focus. I haven't had such an element in my code, so I added a button. 

    Thanks to you all for your help! 


    ynahmany

    Sunday, March 30, 2014 6:52 AM