Hyperlink on popup control does not find hyperlinked file

-2

For some reason, the following hyperlink on a popup control is NOT opening the google website. The popup correctly shows the link, but when I click on the link, I get the following error. The link used (https://www.google.com/) works fine if I use it directly on a browser. What I may be missing here, and can we make it work?

Error:

enter image description here

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

UPDATE:

More details on the error:

enter image description here

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()
wpf
wpf-controls
asked on Stack Overflow Jan 15, 2021 by nam • edited Jan 16, 2021 by nam

1 Answer

0

It turns out that the issue is actually related to the app not being able to find the location of the default browser (in my case Chrome). I discovered the cause of the issue by going through the following posts:

As the saying goes: Give credit where credit is due. Hence, the credit goes to the following users:

  1. Felipe Oriani on his this post on: What to do if your app is unable to find the path of your browser
  2. Ahmad Hindash on his this post on how to find the location of a browser
  3. Dxiv on details on why sometimes you may get the above error.

Based on my reading of the above posts, I was able to resolve the issue as follows:

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

NOTE: For now, I will not mark my response as an answer because other users may come up with a better solution (and in that case I will mark his/her response as an answer).

answered on Stack Overflow Jan 16, 2021 by nam

User contributions licensed under CC BY-SA 3.0