【问题标题】:Hyperlink on popup control does not find hyperlinked file弹出控件上的超链接找不到超链接文件
【发布时间】: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


【解决方案1】:

事实证明,该问题实际上与应用程序无法找到默认浏览器的位置有关(在我的情况下为 Chrome)。我通过阅读以下帖子发现了问题的原因:

俗话说:Give credit where credit is due。因此,功劳归于以下用户:

  1. Felipe Oriani 在他的 this 上发帖:如果您的应用无法找到浏览器的路径怎么办
  2. Ahmad Hindash 在他的 this 帖子中关于如何找到浏览器的位置
  3. 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());
}

注意:目前,我不会将我的回复标记为答案,因为其他用户可能会提出更好的解决方案(在这种情况下,我会将他/她的回复标记为答案) .

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2010-09-17
    • 1970-01-01
    • 2012-04-27
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    相关资源
    最近更新 更多