【发布时间】:2021-01-15 21:50:39
【问题描述】:
由于某种原因,popup 控件上的以下 hyperlink 未打开 google 网站。弹出窗口正确显示了链接,但是当我单击链接时,出现以下错误。如果我直接在浏览器上使用,使用的链接 (https://www.google.com/) 可以正常工作。我可能在这里遗漏了什么,我们可以让它工作吗?
错误:
MainWindow.xaml:
.....
<Grid>
<TextBlock TextWrapping="Wrap">text <Run TextDecorations="Underline" MouseEnter="run_MouseEnter">link</Run>
</TextBlock>
<Popup Name="popLink" StaysOpen="False" Placement="Mouse" MaxWidth="200" PopupAnimation="Slide" AllowsTransparency = "True">
<Border BorderBrush="Beige" BorderThickness="2" Background="White">
<TextBlock Margin="10" TextWrapping="Wrap"> <Hyperlink Click="Hyperlink_Click" NavigateUri="https://www.google.com/">More info</Hyperlink>
</TextBlock>
</Border>
</Popup>
</Grid>
MainWindow.xaml.cs:
private void run_MouseEnter(object sender, MouseEventArgs e)
{
popLink.IsOpen = true;
}
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString());
}
更新:
有关错误的更多详细信息:
System.ComponentModel.Win32Exception
HResult=0x80004005
Message=The system cannot find the file specified.
Source=System.Diagnostics.Process
StackTrace:
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
at WpfApp2.MainWindow.Hyperlink_Click(Object sender, RoutedEventArgs e) in C:\DotNET_Gen\WPF_Core\WpfApp2\MainWindow.xaml.cs:line 45
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.ContentElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Documents.Hyperlink.OnClick()
at System.Windows.Documents.Hyperlink.DispatchNavigation(Object sender)
at System.Windows.Documents.Hyperlink.OnMouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
at System.Windows.Documents.Hyperlink.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.ContentElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at WpfApp2.App.Main()
【问题讨论】:
-
能否请您复制异常详细信息并将此信息添加到帖子中?
-
@Jackdaw 应您的要求,我刚刚添加了一个 UPDATE 部分。
-
因为这段代码是在我的电脑上运行的,我只能提示找不到启动浏览器所需的文件之一。不幸的是,您的
StackTrace日志不包含有关导致此异常的文件的任何信息。 1) 检查系统事件日志。也许它包含导致此失败的更详细信息。 2) 尝试更改默认网络浏览器。 -
你试过
UseShellExecute属性吗? -
@PavelAnikhouski 您的建议有效(谢谢)。我发现您的建议对
.NET Core(我正在使用)来说是更好的建议。你可能想把它写成response(也许,用一行解释),这样其他读者可以更好地从中受益。而且,我会将其标记为Answer。
标签: wpf wpf-controls