WPF-DataGridHyperlinkColumn无法打开本地HTML文件 [英] WPF - DataGridHyperlinkColumn not opening local HTML file

查看:162
本文介绍了WPF-DataGridHyperlinkColumn无法打开本地HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境 VS2019-版本16.4.2 Windows 10-Pro 1903版 WPF Core 3.1

为什么以下代码无法从<$ c $中的链接打开文件我的 WPF 应用中的c> DataGridHyperlinkColumn 。根据此帖子,它应该打开文件-除非我在这里丢失了内容。

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

备注


  1. 文件在那里,我可以通过将以下链接直接粘贴到Windows资源管理器中来手动打开它: file:/// C:/ Users /MyUserName/AppData/Local/Temp/TestLocalFile.htm 。它会在我的默认浏览器 Google Chrome 中打开。

  2. 根据此发布文件应打开。

  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.

错误



System.ComponentModel.Win32Exception
HResult = 0x80004005
消息=系统找不到指定的文件。
Source = System.Diagnostics.Process ....
.....

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



推荐答案

您应该更新代码,该代码会按以下顺序启动链接

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 在这里起到了神奇的作用,在.NET Core中默认将其设置为 false


UseShellExecute 为false, FileName 属性可以是可执行文件的
完全限定路径,也可以是简单的可执行文件命名系统将尝试在
PATH环境变量指定的文件夹中查找的

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.

case文件名不是可执行文件,因此要使其正常工作,必须将 UseShellExecute 设置为 true ,这是正确的

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.

附加的答案已使用8年,使用的是经典.NET,此属性为 true 默认情况下。您也可以参考此GitHub 线程了解详情

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

这篇关于WPF-DataGridHyperlinkColumn无法打开本地HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆