PreviewKeyDown with System.Diagnostics.Process.Start(some url) opens two browsers

跟風遠走 提交于 2019-12-13 02:58:17

问题


In this question (and accepted answer) a simple WPF program sets up a PreviewKeyDown handler to call Process.Start on a directory name to open that folder in Windows File Explorer.

If I change the handler definition to open up a URL as follows:

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

it opens up two browser tabs when I press the o key.

The original version did not open two File Explorer windows.

Why does the URL version open two browser windows? What's a good way to get it to only open one?

To make this concrete and more explicit, here's an entire program which demonstrates the issue.

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

Here's what the example program looks like:


UPDATE in response to comments below

If I add a breakpoint at the following line:

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

Only one browser tab opens! Very strange that the behaviour is different when debugging.


UPDATE in response to comments below

If I change the browser that is used to Microsoft Edge as follows:

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

it still opens up two tabs.


UPDATE

The issue seems to be intermittent. Most of the time, it opens two browser tabs. However, occasionally it will open only one tab.


回答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;**
};



回答2:


This approach seems to resolve the issue:

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

Note that it's a slight modification of the answer by @user11344985. It moves the e.Handled = true; line into the if body.



来源:https://stackoverflow.com/questions/55624087/previewkeydown-with-system-diagnostics-process-startsome-url-opens-two-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!