0

I am opening a PDF file using below code:

System.Windows.Controls.Frame frame = new System.Windows.Controls.Frame();
System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();
browser.Unloaded += Browser_Unloaded;
Uri myUri = new Uri("C:\Users\MyUser\Downloads\myPDFFile.pdf", UriKind.RelativeOrAbsolute);
browser.Navigate(myUri);
frame.Content = browser;

And below the event handler for when Browser is unloaded:

private void Browser_Unloaded(object sender, System.Windows.RoutedEventArgs e)
{
    browser = frame.Content as System.Windows.Controls.WebBrowser;
    browser.Dispose();
    frame.Content = null;
}

What I am trying to do is to handle when browser is closed but above event handler is not being fired.

Jesse
  • 1,386
  • 3
  • 9
  • 23
Willy
  • 9,848
  • 22
  • 141
  • 284
  • How are you trying to "close" it? According to the documentation, the `Unloaded` event fires *"when the element is removed from within an element tree of loaded elements"*, so the event should fire if you remove it from the frame. – Jesse Dec 16 '22 at 02:09
  • @Jesse ah ok, so unloaded event is not what i am searching for.... i want that once the browser showing the pdf is closed i can handle this event and do some stuff. is it possible? – Willy Dec 16 '22 at 14:54
  • I'm confused what you mean by "close". The web browser control itself doesn't appear to provide any way to close it. Are you talking about closing the application that the browser control is a part of as a whole? If so, you can use the [`Application.Exit` event](https://stackoverflow.com/a/20347268/10601203). – Jesse Dec 16 '22 at 17:09

0 Answers0