Silverlight 4: Detect browser F5 / refresh and X / Close

前端 未结 3 1948
遇见更好的自我
遇见更好的自我 2020-12-16 17:52

I want to determine how to filter F5, refresh button, X and close in browser via silverlight 4.0 or even in server side.

thank you

EDITED:

3条回答
  •  北海茫月
    2020-12-16 18:17

    There is no property to check if your application is loaded by pressing the F5-button but you could handle the application startup event and set a variable with a datetime. The moment your page gets loaded you can check if the timespan is just a couple of seconds ago. So now you know that the application is loaded the first time or the F5-button is pressed when that time it is only a couple of seconds ago. I don't know if this is sufficient for you but you can give it a try:

    App.xaml.cs

    public class App : Application
    {
     private DateTime appStartupTime {get; set};
     public App()
     {
         Startup += new EventHandler(Application_Startup);
     } 
    
     void Application_Startup(object sender, StartupEventArgs e)
     {
       //initialize the startupTime
       appStartupTime = DateTime.Now; 
     }
     public bool IsApplicationReLoaded
     {
       get
       {
         //return true if your app is started less 10 seconds ago
         return DateTime.Now.AddSeconds(-10) < appStartupTime;
       }
      }
    }
    

    Now you can start using the code below from everywhere

    (Application.Current as App).IsApplicationReloaded
    

提交回复
热议问题