【问题标题】:Open Browser with URL WPF使用 URL WPF 打开浏览器
【发布时间】:2020-05-27 14:46:12
【问题描述】:

如何通过超链接打开外部浏览器,在浏览器中应该是target='_top'。

WPF netcore 3.1 应用程序中的代码是什么?

  1. 使用命令参数

    <TextBlock>
        <Hyperlink CommandParameter="{Binding ExternalURL}"
                   Command="{Binding NavHomeViewCommand}" >Open in Browser ...
        </Hyperlink>
    </TextBlock>
    
  2. 更改 DelegateCommand 以使用对象参数(使用 prismlibrary mvvm 模式)

    navHomeViewCommand = new DelegateCommand<object>(NavHomeView);
    
  3. 命令属性:

    public string ExternalURL{ get => "https://www.google.com/";}
    private readonly ICommand navHomeViewCommand;
    public ICommand NavHomeViewCommand
    {
        get { return navHomeViewCommand; }
    }
    
  4. 打开浏览器

    private void NavHomeView(object ID)
    {
        if(obj is string destinationurl)
            System.Diagnostics.Process.Start("https://google.com");  //???????   
    }
    

抛出“未知可执行文件”异常。

【问题讨论】:

  • 看看能不能解决你的问题
  • .netcore3.1 不是你日常使用的 WPF
  • “重复”问题中的答案均无效。经过多次试验和错误,得到的“hack”是: System.Diagnostics.Process.Start("cmd", "/C start" + " " + destinationurl);
  • 我重新打开这个问题,你可以自己回答
  • 您可能还想标记这个。 .netcore 3.1

标签: c# wpf browser .net-core-3.1


【解决方案1】:
System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe", "https://www.google.fr");

或者如果你不知道导航器.exe文件的路径

var UrlExterne = "https://www.google.fr";
var filetemp = $"{Path.GetTempFileName()}.url";
try
{
     using (var sw = new StreamWriter(filetemp))
     {
          sw.WriteLine("[InternetShortcut]");
          sw.WriteLine($"URL={UrlExterne}");
          sw.Close();
      }
      System.Diagnostics.Process.Start(filetemp);
 }
 finally
 {
     if(File.Exists(filetemp))
          File.Delete(filetemp);
 }

【讨论】:

    【解决方案2】:

    使用指定 URL 打开 Windows 操作系统级别默认浏览器的解决方案。通过WPF Github issue 2566推荐:

            var destinationurl = "https://www.bing.com/";
            var sInfo = new System.Diagnostics.ProcessStartInfo(destinationurl)
            {
                UseShellExecute = true,
            };
            System.Diagnostics.Process.Start(sInfo);
    

    旧: 打开无提示 CMD 提示并打开 Windows 操作系统默认浏览器的解决方案:

        private void NavHomeView(object ID)
        {
            //return;
            if (IDis string destinationurl)
            {
                var link = new Uri(destinationurl);
                var psi = new ProcessStartInfo
                {
                    FileName = "cmd",
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    Arguments = $"/c start {link.AbsoluteUri}"
                };
                Process.Start(psi);
            ...
    

    但如果您打算使用 MSIX 打包项目类型打包应用并将其放入 Windows 应用商店,那么您将 fail certification 使用 CMD.EXE。

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多