使用 URL WPF 打开浏览器

OzB*_*Bob 3 c# browser wpf .net-core-3.1

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

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

  1. 使用命令参数

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

    navHomeViewCommand = new DelegateCommand<object>(NavHomeView);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 命令属性:

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

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

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

OzB*_*Bob 8

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

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

旧: 打开静默 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);
        ...
Run Code Online (Sandbox Code Playgroud)

但如果您的目的是使用 MSIX 打包项目类型打包应用程序并将其放入 Windows 应用商店,那么您将无法通过使用 CMD.EXE 的认证。