开发者

How to handle the Before Close Event in a WPF Browser Application?

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜