Thursday, March 10, 2011

Application_Exit event - App.xaml (Silverlight)

Before the Silverlight application exit, you can confirm whether the user really wants to exit or not.


Follow this Steps:

1.) Register ScriptableObject in the App.xaml.cs file


private void Application_Startup(object sender, StartupEventArgs e) 
{ 
MainPage mainPage = new MainPage(); 
HtmlPage.RegisterScriptableObject("myApp", mainPage); 

this.RootVisual = mainPage; 
} 

2.) Create ScriptableMember (method) in your Silvelright application (MainPage.xaml)

[ScriptableMember] 
public void AskToSaveChanges() 
{ 
   if (MessageBox.Show("Save changes?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 
     { 
      // Save Changess 
     } 
} 

3.) Now, in the aspx page that is hosting the Silverlight application

<script type="text/javascript">     
    window.onbeforeunload = saveBeforeExit;     
    function saveBeforeExit() { 
       var slApp = document.getElementById("slApp")     
       slApp.Content.myApp.AskToSaveChanges(); 
    }
</script>

Note: "slApp" is the ID of the object tag which is hosting the Silvelright application.

Hope this helps.

0 comments: