【发布时间】:2021-01-11 05:13:19
【问题描述】:
我正在使用 c# 开发一个简单的 WPF 图像查看器。我正在添加一项功能,因此您可以右键单击图像以打开文件路径。我正在使用Process.Start 以路径作为参数打开资源管理器。我的应用运行良好,但无法打开资源管理器。
我的c#代码是
string selectedFileName;
string directoryPath;
private void MenuItemFromFile_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files (*.jpg|*.jpg|*.gif|*.ico|*.bmp|*.png|*.wdp| .tiff)|All files (*.*)|*.*";
openFileDialog.FilterIndex = 8;
if (openFileDialog.ShowDialog() == true)
{
selectedFileName = openFileDialog.FileName;
directoryPath = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedFileName);
bitmap.EndInit();
FullImage.Source = bitmap;
}
}
private void ContextItemFileLocation_OnClick(object sender, RoutedEventArgs e)
{
Process.Start("C:\\Windows\\explorer.exe", @directoryPath);
}
我的 XAML
<Image Grid.Row="2"
Grid.Column="1"
Name = "FullImage"
Stretch="Uniform"
StretchDirection="Both" >
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="open file location"
Name="ContextItemFileLocation"
Click="ContextItemFileLocation_OnClick"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
【问题讨论】:
-
您能描述一下单击
ContextItem时究竟发生了什么吗?它是有线的,因为我已经测试了代码并且它对我来说工作正常。在运行Process.Start()之前,您是否尝试过调试以检查directoryPath的值? -
目录路径是调试时应该是的,当我点击上下文菜单时,它关闭了,没有任何反应
-
您确定在尝试打开图像位置之前先选择了图像吗?请在
Process.Start()之前放置一个BreakPoint 并调试以检查directoryPath的值,甚至是否调用了事件处理程序ContextItemFileLocation_OnClick。