带System.Diagnostics.Process.Start(某些URL)的PreviewKeyDown打开两个浏览器

时间:2019-04-11 03:06:14

标签: c# wpf process.start

this question(和可接受的答案)中,一个简单的WPF程序设置了一个PreviewKeyDown处理程序,以对目录名称调用Process.Start来在Windows File Explorer中打开该文件夹。

如果我更改处理程序定义以打开URL,如下所示:

data_grid.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.O && data_grid.SelectedItem is DirectoryInfo info)
        System.Diagnostics.Process.Start("https://www.github.com");
};

当我按下o键时,它会打开两个浏览器选项卡。

原始版本没有打开两个 File Explorer 窗口。

为什么URL版本会打开两个浏览器窗口?使它仅打开一个的好方法是什么?

为使这一点更加具体和明确,这里有一个演示该问题的完整程序。

MainWindow.xaml

<Window x:Class="WpfUrlsDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUrlsDataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfUrlsDataGrid
{
    public class Address { public string Url { get; set; } }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var data_grid = new DataGrid()
            {
                IsReadOnly = true,
                AutoGenerateColumns = true,
                ItemsSource = new[] 
                {
                    new Address() { Url = "https://www.google.com" },
                    new Address() { Url = "https://www.github.com" },
                    new Address() { Url = "https://www.stackoverflow.com" }
                }
            };

            data_grid.PreviewKeyDown += (s, e) =>
            {
                if (e.Key == Key.O && data_grid.SelectedItem is Address address)
                    System.Diagnostics.Process.Start(address.Url);
            };

            var dock_panel = new DockPanel();

            dock_panel.Children.Add(data_grid);

            Content = dock_panel;
        }
    }
}

示例程序如下所示:

enter image description here


更新以回应以下评论

如果我在以下行添加断点:

System.Diagnostics.Process.Start(address.Url);

仅打开一个浏览器选项卡!很奇怪,调试时的行为不同。


更新以回应以下评论

如果按照以下方式更改用于Microsoft Edge的浏览器:

System.Diagnostics.Process.Start(String.Format("microsoft-edge:{0}", address.Url));

它仍然会打开两个标签。


更新

该问题似乎是间歇性的。 大部分时间,它将打开两个浏览器标签。但是,偶尔它只会打开一个标签。

2 个答案:

答案 0 :(得分:1)

data_grid.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.O && data_grid.SelectedItem is DirectoryInfo info)
        System.Diagnostics.Process.Start("https://www.github.com");
    **e.Handled = true;**
};

答案 1 :(得分:0)

这种方法似乎可以解决问题:

class model : TableEntity{
    public string Name { get; set; }
    public override string ToString(){
     return  " " + Name;
    }
}
static async Task<TableResult>  GetAllMessages(CloudTable table, String InvocationName)
{

    TableResult x = await table.ExecuteAsync(TableOperation.Retrieve(InvocationName,"1" ));    
    return x;
}

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route =null)] HttpRequest req,ILogger log)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference("models");
    var x = await GetAllMessages(table, "InvocationName");
    string url = ((model)x.Result).ToString();
}

请注意,这是@ user11344985对答案的轻微修改。它将data_grid.PreviewKeyDown += (s, e) => { if (e.Key == Key.O && data_grid.SelectedItem is Link link) { System.Diagnostics.Process.Start((data_grid.SelectedItem as Link).Url); e.Handled = true; } }; 行移动到if正文中。