C# UWP Open web Url with Microsoft Edge
Asked Answered
A

3

8

I want open a URL using Microsoft Edge in my UWP. Searching, I found this code:

using System.Diagnostics; 
using System.ComponentModel; 

private void button_Help_Click(object sender, RoutedEventArgs e)
{
    Process.Start("microsoft-edge:http://www.bing.com");
}

But it shows the following error:

The name Process do not exist in the current context

If I press Ctrl+., it only shows generate class options.

Any help is appreciated.

Arson answered 14/7, 2016 at 4:34 Comment(0)
K
27

Process.Start is a traditional method used in .NET Framework which can't be used in UWP apps directly. To open web URI with Microsoft Edge in UWP, we can use Launcher.LaunchUriAsync method. For example:

// The URI to launch
string uriToLaunch = @"http://www.bing.com";

// Create a Uri object from a URI string 
var uri = new Uri(uriToLaunch);

// Launch the URI
async void DefaultLaunch()
{
   // Launch the URI
   var success = await Windows.System.Launcher.LaunchUriAsync(uri);

   if (success)
   {
      // URI launched
   }
   else
   {
      // URI launch failed
   }
}

However this will open the URI with the default web browser. To always open it with Microsoft Edge, we can use Launcher.LaunchUriAsync(Uri, LauncherOptions) method with specified LauncherOptions.TargetApplicationPackageFamilyName property. TargetApplicationPackageFamilyName property can specify the target package that should be used to launch a file or URI. For Microsoft Edge, its Package Family Name is "Microsoft.MicrosoftEdge_8wekyb3d8bbwe". Following is an example shows how to use this.

// The URI to launch
string uriToLaunch = @"http://www.bing.com";
var uri = new Uri(uriToLaunch);

async void LaunchWithEdge()
{
   // Set the option to specify the target package
   var options = new Windows.System.LauncherOptions();
   options.TargetApplicationPackageFamilyName = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe";

   // Launch the URI
   var success = await Windows.System.Launcher.LaunchUriAsync(uri, options);

   if (success)
   {
      // URI launched
   }
   else
   {
      // URI launch failed
   }
}
Kerman answered 14/7, 2016 at 9:9 Comment(4)
Thank you, all sound logic, but I dont know why I am getting error: "The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'." in Windows.System.Launcher.LaunchUriAsync line.Arson
@Julius This is a strange error. I tested the code in my side, and it works well. After a few search, I found a similar issue here, please check IAsyncAction in this blog. Hope it helps.Kerman
Thank you for your link, in my side I found similar things, clean install.Arson
Is there any link which captures the return result from the browser which is opened by LaunchUriAsyncGodbeare
A
4

You can do it, but Microsoft Edge must be your default browser. See the code bellow

private async void launchURI_Click(object sender, RoutedEventArgs e)      
{       
     // The URI to launch
     var uriBing = new Uri(@"http://www.bing.com");

     // Launch the URI
     var success = await Launcher.LaunchUriAsync(uriBing);             
}
Approve answered 14/7, 2016 at 4:47 Comment(0)
M
1

I have tried this and it works for me without setting Edge as default browser:

 await Launcher.LaunchUriAsync(new Uri("microsoft-edge:https://www.bing.com"));
Miriam answered 2/11, 2021 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.