0

I am just starting with a WPF Browser application.

I would like to handle the Before Close Event of the browser, so how would i handle that event?

0

2 Answers 2

3

If i understood your question correctly you can:

  • add your BrowserControl tosome Window and handle Window's Closing event.
  • or you can try variations of Unloaded event(this one all FrameworkElement objects have).

Update
Sorry, just came back.. Example:

yourControl.Unloaded += (s, e) =>
            {
                // some code here
                e.Handled = true;
            };
4
  • Thanks for the response. The first option will not work for me. The second one sound good. Can u maybe post an example?
    – Willem
    Sep 1, 2011 at 5:50
  • No luck. I added a method in there, but nothing happens. This is my code: this.Unloaded += (s, e) => { CloseTabs(); e.Handled = true; }; Is that correct?
    – Willem
    Sep 1, 2011 at 6:34
  • Yes, but it depends on what CloseTabs() does. Or maybe Unloaded is not firing at all...Try to use debugger and find out problem.
    – 0x49D1
    Sep 1, 2011 at 9:57
  • I just checked. It only fires when u navigate back. Not when i click the close button? Any other idea what i can try next?
    – Willem
    Sep 1, 2011 at 10:03
1

I found this solution.

In the App.xaml.cs file:

 private void Application_Onexit(object sender, ExitEventArgs e)

        { 

          //write your code in here!

        }

In the App.xaml file:

<Application x:Class="(yourclasss)"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup"
             Exit="Application_Onexit"
             ShutdownMode="OnLastWindowClose"
             StartupUri="startup.xaml">

So basically the ShutdownMode property needs to be set in order to make it work.

Then add a event in app.xaml.cs:

public static event EventHandler UnloadPageWorkaround;

public void Application_Onexit(object sender, ExitEventArgs e)
{
    UnloadPageWorkaround.Invoke(null, null);
}

Then on the relevant page:

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {

        WPFBrowser.App.UnloadPageWorkaround += new EventHandler(DoMySpecialPageCleanupStuff);
    }

    void DoMySpecialPageCleanupStuff(object sender, EventArgs e)
    {
        //do cleanup
    }       

The only problem with this is, you can't stop the application from exiting.

Hope it helps.

2
  • You can put your code in Closed/Closing event of main window then..Effect will be same, code-simplier. What did you mean by asking about 'before closing browser'? Web browser or browser control from wpf, located in another window?..Sorry, but when i was answering-i thought that you were talking about webbrowser CONTROL.
    – 0x49D1
    Sep 1, 2011 at 12:32
  • @0x49D1 No, it is a XBAP WPF Browser Application. So there is no window or WebBrowser Control, only Pages. So, i want a similar event like Window_Closing (On a Window) for the browser(something like: Browser_Closing), but it seems there is none...
    – Willem
    Sep 1, 2011 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.