Do you know how to prevent users from losing changes in your Silverlight application, if they click the "back button" or "close browser" button?
#1
Add this method to your main page (if using Silverlight navigation framework) or user control (if Silverlight app)
public void RegisterOnBeforeUnload() { //Register Silverlight object for availability in Javascript. const string scriptableObjectName = "SilverlightClientControl"; // <-- THIS IS THE NAME OF YOUR HOSTING OBJECT IN THE WEB PAGE HtmlPage.RegisterScriptableObject(scriptableObjectName, this); //Start listening to Javascript event. string pluginName = HtmlPage.Plugin.Id; HtmlPage.Window.Eval(string.Format( @"window.onbeforeunload = function () {{ var slApp = document.getElementById('{0}'); var result = slApp.Content.{1}.OnBeforeUnload(); if(result.length > 0) return result; }}", pluginName, scriptableObjectName) ); }
Figure: Method that will register your Silverlight object for availability in JavaScript and start listening on JavaScript events
#2
Call the top method "RegisterOnBeforeUnload" in the constructor of your control
#3
Add this to the same user control
[ScriptableMember] public string OnBeforeUnload() { if (MainViewModel.HasUnsavedChanges) return "You have unsaved changes! Please save them before leaving the application."; return string.Empty; }
Figure: "MainViewModel.HasUnsavedChanges" is the flag if the exit is allowed without warning or not (= user clicks on "Exit Auction" which exits normally)
#5
Done
Figure: Now when you want the users is in the middle of a change and navigates away, he gets a nice warning!
This fires on the "Back button", "Close tab" or "Close browser", which is awesome!
PS
You could even automagically save the changes when the user closes the browser!!
12 comments:
Shouldn't this be..
if (MainViewModel.HasUnsavedChanges == true)
...
Well spotted Juri
This happens when you have to fix production code for blog posts...
Nice ! but how to the code for the page ~!
This is a great post - very helpful - Thanks
Perfect! Thank you :)
Hi Peter Gfader,
I got below problem when am using ur above code.
Got Problem in the line of code:
-----------------------------------
HtmlPage.RegisterScriptableObject(scriptableObjectName, this);
Erroe is:
-----------------------------------
Object does not have a ScriptableAttribute or any scriptable members.
Please solve my prolem ASAP. Thanks in advance.
-Ashok
@Ashok
Check that the Silverlight object has an ID in the hosting page.
Check that you are using the right ID.
See this hint in code above:
// <-- THIS IS THE NAME OF YOUR HOSTING OBJECT IN THE WEB PAGE
Many thanks Peter!
Hi Peter.
may you explain MainViewModel.HasUnsavedChanges
i am not getting this line where you have declare this hasUnsavedChanges?
Please reply as soon as possible.
@Ajay
HasUnsavedChanges is a property on your ViewModel that tells you if the ViewModel is in "dirty" state.
you can just "return true" for testing purposes....
Hi again.
i am new in silverlight and i am fresher in development field. my boss told me that you have to do any how @boss"i don't know how you will do".
so my question is when we will set the property true to the HasUnsavedChanges or we have to set that property manually to true when user is going to type any thing on the silverlight page.
please help me peter sir. otherwise i don't know what will happen with me.
May you send me the example if it is possible.
Thanks,
Ajay Singh
@Ajay
If you are using a "fancy" framework like Prism you might be able to just use "IsDirty" on the Object that you are binding to.
Otherwise manual. You might be able to solve this elegantly by hooking into INotifyPropertyChanged
Post a Comment