WPF - DataGridHyperlinkColumn not opening local HTML file

▼魔方 西西 提交于 2020-01-16 13:04:24

问题


Environment: VS2019 - ver 16.4.2, Windows 10 - Pro ver 1903, WPF Core 3.1

Why the following code is not opening the file from the link in DataGridHyperlinkColumn in my WPF app. According to this post it should open the file - unless I'm missing something here.

XAML

<Window x:Class="WPF_DataGrid_EFCore.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:WPF_DataGrid_EFCore"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dgTest" AutoGenerateColumns="False" Margin="0,43,0,0">
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Title" Binding="{Binding Title}">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <EventSetter Event="Hyperlink.Click" Handler="dgTest_HyperLink_Click"/>
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="btnTest" Content="Test" HorizontalAlignment="Left" Margin="355,6,0,0" VerticalAlignment="Top" Click="btnTest_Click"/>
    </Grid>
</Window>

MainWindow.Xaml.cs

private void dgTest_HyperLink_Click(object sender, RoutedEventArgs e)
{
    Hyperlink link = (Hyperlink)e.OriginalSource;
    Console.WriteLine(link.NavigateUri.AbsoluteUri); //correctly shows the path as file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm
    System.Diagnostics.Process.Start(link.NavigateUri.AbsoluteUri);
}

Remark

  1. The file is there and I can manually open it by pasting the following link directly to the Windows Explorer: file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm. It opens in my default browser Google Chrome.
  2. According to this post the file should open.

Error

System.ComponentModel.Win32Exception HResult=0x80004005 Message=The system cannot find the file specified. Source=System.Diagnostics.Process .... .....


回答1:


You should update the code, which is launching link per follows

private void dgTest_HyperLink_Click(object sender, RoutedEventArgs e)
{
    Hyperlink link = (Hyperlink)e.OriginalSource;
    Console.WriteLine(link.NavigateUri.AbsoluteUri); //correctly shows the path as file:///C:/Users/MyUserName/AppData/Local/Temp/TestLocalFile.htm

    var psi = new ProcessStartInfo
    {
        FileName = link.NavigateUri.AbsoluteUri,
        UseShellExecute = true
    };
    Process.Start(psi);
}

UseShellExecute does the magic here, in .NET Core it's set to false by default, per MSDN and

When UseShellExecute is false, the FileName property can be either a fully qualified path to the executable, or a simple executable name that the system will attempt to find within folders specified by the PATH environment variable.

In your case file name isn't executable, so for make it working you have to set UseShellExecute to true, this is correct for .NET Core apps.

The attached answer is 8 years old and using the classic .NET, where this property is true by default. You can also refer to this GitHub thread



来源:https://stackoverflow.com/questions/59481361/wpf-datagridhyperlinkcolumn-not-opening-local-html-file

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