【问题标题】:C# UWP Open web Url with Microsoft EdgeC# UWP 使用 Microsoft Edge 打开 Web 网址
【发布时间】:2016-11-16 20:38:53
【问题描述】:

我想在我的 UWP 中使用 Microsoft Edge 打开一个 URL。搜索,我找到了这段代码:

using System.Diagnostics; 
using System.ComponentModel; 

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

但它显示以下错误:

当前上下文中不存在名称进程

如果我按Ctrl+.,它只显示生成类选项。

感谢任何帮助。

【问题讨论】:

    标签: c# url uwp microsoft-edge


    【解决方案1】:

    您可以这样做,但 Microsoft Edge 必须是您的默认浏览器。请看下面的代码

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

    【讨论】:

      【解决方案2】:

      Process.Start 是 .NET Framework 中使用的传统方法,不能直接在 UWP 应用中使用。要在 UWP 中使用 Microsoft Edge 打开 Web URI,我们可以使用 Launcher.LaunchUriAsync method。例如:

      // 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
         }
      }
      

      但是,这将使用默认 Web 浏览器打开 URI。要始终使用 Microsoft Edge 打开它,我们可以使用 Launcher.LaunchUriAsync(Uri, LauncherOptions) method 和指定的 LauncherOptions.TargetApplicationPackageFamilyName propertyTargetApplicationPackageFamilyName 属性可以指定应该用于启动文件或 URI 的目标包。对于 Microsoft Edge,其包系列名称“Microsoft.MicrosoftEdge_8wekyb3d8bbwe”。下面是一个例子展示了如何使用它。

      // 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
         }
      }
      

      【讨论】:

      • 谢谢,所有合理的逻辑,但我不知道为什么会出现错误:“类型'IAsyncAction'是在未引用的程序集中定义的。您必须添加对程序集的引用'Windows ,版本=255.255.255.255,文化=中性,PublicKeyToken=null,ContentType=WindowsRuntime'。"在 Windows.System.Launcher.LaunchUriAsync 行中。
      • @Julius 这是一个奇怪的错误。我测试了我身边的代码,它运行良好。经过几次搜索,我发现了一个类似的问题here,请查看此博客中的IAsyncAction。希望对您有所帮助。
      • 感谢您的链接,在我身边我发现了类似的东西,全新安装。
      • 是否有任何链接从 LaunchUriAsync 打开的浏览器捕获返回结果
      【解决方案3】:

      我已经尝试过了,它适用于我,无需将 Edge 设置为默认浏览器:

       await Launcher.LaunchUriAsync(new Uri("microsoft-edge:https://www.bing.com"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多