lol 怎么得gemstoneking官网

Integration of browser’s unloading with Silverlight - Silverlight, WP7, .NET, C#, ASP.NET MVC
It happens very often that people, the users of your application loose their changes because the accidentally closed a browser window or browser tab. Specially because all browsers now support tabs, which is not something all non-technical users completely understand.
There is however something you can use to partly overcome this issue. In the year 2004 there was an article . This article explains excellently how to make use of the window.onbeforeunload event in Javascript.
I thought about this a few minutes and got the following example on how to integrate this inside your Silverlight application.
First of all I wanted this window.onbeforeunload Javascript event to call a method inside my Silverlight application. So I wrote a small method that can be called inside Silverlight.
    1 [ScriptableMember]
    2 public string OnBeforeUnload()
    3 {
    4     return &Warning you're closing...&;
    5 }
Besides that it also needs to be registrated in Javascript.
const string scriptableObjectName = &Bridge&;
HtmlPage.RegisterScriptableObject(scriptableObjectName, this);
So now we have a method that can be called from Javascript. Let’s start listening to this Javascript event from Silverlight.
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)
                   
            }}&, pluginName, scriptableObjectName)
    );
This does work. But do we want to always show a message? Or do we only want to show the message if it’s relevant? I think the choice will be the last, at least for me it will. So what about an IsDirty check? I created a very simple non-real one. Because of the sake of the article, it’s about the message when closing, right?
private bool isDirty = true;
public bool IsDirty
    get
    {
        bool tempDirty = this.isD
        this.isDirty = false;
        return tempD
    }
Let’s combine this with our OnBeforeUnload method in Silverlight.
[ScriptableMember]
public string OnBeforeUnload()
    if (IsDirty)
        return &The page is dirty. Are you sure you want to close?&;
    return string.E
When you take a close look at my implementation of IsDirty, you’ll see it’s only dirty the first time, the second time it’s not. Now when you try to exit the page by one of the following ways, you’ll get this message:
By closing T
By closing W
By changin
When you press Cancel you will actually stay on the page. The thing is we only have influence on the text in the middle. I have been thinking about different solutions, by using for example a dialog in Silverlight, but sadly this is just not possible. The function window.onbeforeunload only accepts a message that it can show. When nothing is return on the event, it just closes.
For your convenience the complete code of Page.xaml.cs.
    1 public partial class Page : UserControl
    2 {
    3     private bool isDirty = true;
    4     public Page()
    5     {
    6         InitializeComponent();
    7         RegisterOnBeforeUnload();
    8     }
    9 
   10     public void RegisterOnBeforeUnload()
   11     {
   12         //Register Silverlight object for availability in Javascript.
   13         const string scriptableObjectName = &Bridge&;
   14         HtmlPage.RegisterScriptableObject(scriptableObjectName, this);
   15         //Start listening to Javascript event.
   16         string pluginName = HtmlPage.Plugin.Id;
   17         HtmlPage.Window.Eval(string.Format(
   18             @&window.onbeforeunload = function () {{
   19                 var slApp = document.getElementById('{0}');
   20                 var result = slApp.Content.{1}.OnBeforeUnload();
   21                 if(result.length & 0)
   22                    
   23             }}&,pluginName,scriptableObjectName)
   24             );
   25     }
   26 
   27     [ScriptableMember]
   28     public string OnBeforeUnload()
   29     {
   30         if (IsDirty)
   31             return &The page is dirty. Are you sure you want to close?&;
   32         return string.E
   33     }
   34 
   35     public bool IsDirty
   36     {
   37         get
   38         {
   39             bool tempDirty = this.isD
   40             this.isDirty = false;
   41             return tempD
   42         }
   43     }
   44 }

我要回帖

更多关于 gemstone 的文章

 

随机推荐