locked
running Launcher.LaunchUriAsync when app loads in C# Metro 8.1

    Question

  • I'm working on an app, which doesn't have many control's etc. and as the user may need access to a web browser to get a url. I want to have the app start IE when it starts, so my app would be on the left, and IE would take the rest of the screen. I've looked at using webview, but the url's don't render correctly using that.

    So in essence I want my app to start and then to kick off

    await Launcher.LaunchUriAsync(new Uri("test.com"));

    It works fine if I use a button, but I'd like it to just kick off automatically in the UI thread.

    Wednesday, March 26, 2014 8:10 PM

Answers

  • Hello,

    The Launch will only happen when your app is fully visible to the user. This question was asked on another forum thread: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/e947529b-3806-47ad-bbe7-21c842e0c865/launchuriasync-only-work-on-debug-mode?forum=winappswithcsharp and you can take a look at my response to see how you can implement such functionality.

    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 8:44 PM
    Moderator