问题描述
出于某种原因,hyperlink
控件上的以下 popup
无法打开 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,Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEvent newEvent)
at System.Windows.UIElement.onmouseupThunk(Object 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,Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,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,Int32 numArgs,Delegate catchHandler)
at System.Windows.Threading.dispatcher.LegacyInvokeImpl(dispatcherPriority priority,TimeSpan timeout,Delegate method,Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,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()
解决方法
事实证明,问题实际上与应用无法找到默认浏览器的位置有关(在我的情况下为 Chrome
)。我通过阅读以下帖子发现了问题的原因:
俗话说:Give credit where credit is due
。因此,功劳归于以下用户:
-
Felipe Oriani
在他的 this 帖子中:如果您的应用无法找到浏览器的路径该怎么办 -
Ahmad Hindash
在他关于如何查找浏览器位置的 this 帖子中 -
Dxiv
on details on 为什么有时您可能会收到上述错误。
根据我对上述帖子的阅读,我能够解决以下问题:
private void Hyperlink_Click(object sender,RoutedEventArgs e)
{
//PLEASE NOTE: You would want to programmatically find the location of you browser as user `Felipe Oriani` did in his above mentioned post. For brevity,I am leaving that part.
System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",((Hyperlink)sender).NavigateUri.ToString());
}
注意:目前,我不会将我的回复标记为答案,因为其他用户可能会提出更好的解决方案(在这种情况下,我会将他/她的回复标记为答案) .