解决Silverlight F5刷新问题

最近在做一个SL的项目,做完后,遇到一个F5刷新的问题,本人也是第一次接触接触SL项目,记得再ASP.NET浏览器的缓存会自动保存最后一次的浏览记录的。

所以就在网上到处找资料,可惜运气不好,都没找到合适的资料。基本的解决方法都是通过再HTML页面增加JS方法,屏蔽F5刷新按钮的功能,但是这样的需求并不是我们项目中所要的,还好在BAIDU和群里高手的帮助下,终于大体了解了SL刷新的过程和解决F5刷新返回最后次浏览页面的思想。。

1:SL刷新过程

SL本身就是HTML页面的一个插件程序,在浏览器退出或者F5刷新的时候,SL本身首先调用自身的APP.xaml的 Application_Exit 方法,然后再次进入浏览器的时候加载Application_Startup方法,一般都是用Application_Startup 方法去设定SL程序的启动初始界面。

知道了SL运行的过程我想实现F5刷新问题就不难了,

2:F5刷新解决思路

首先在本地建立独立存储(COOKIE也可以,不过不知道为啥用COOKIE的话,SL项目用火狐浏览访问数据就有问题,以待以后研究。)

为了简单阐述过程,本文的COOKIE就保存3个值:

UserID:用户账号信息

PageAddress:最后次浏览页面地址

ExitTime:退出时间  

1>系统初始化加载的时候,判断本地存储是否有数据,并且判断当前时间与上次退出时间差 是否小于5秒,(我这里利用5秒作为刷新和重新登录的标准,当然因人而异)

2>如果上面条件成立,咋直接根据账号信息,加载系统主界面,并将Navigation 的IFRAM 地址指向本地存储的上次保存地址即可。

3>如果不成立,显示登录界面,并清除本地存储数据,登录后重新赋值账号信息

4>伴随着SL界面的浏览,操作界面的时候同时更新本地存储的页面地址值

5>Application_Exit 事件里,增加 slcookie.ExitTime = DateTime.Now.ToString(); 时间的赋值

OK! 整体思路大致如此,现在我将以一个简短的实例来详细说明

一、新建项目SLF5

打开VS2010,新建Silverlight应用程序,

并勾选在新网站中承载应用程序

这样一个SL项目就建立完毕,

接下来需要的就是建立本地存储类,为了便于理解我就取名SLCookie.cs

View Code
  1 using System;
  2 using System.Net;
  3 using System.Windows;
  4 using System.Windows.Controls;
  5 using System.Windows.Documents;
  6 using System.Windows.Ink;
  7 using System.Windows.Input;
  8 using System.Windows.Media;
  9 using System.Windows.Media.Animation;
 10 using System.Windows.Shapes;
 11 using System.IO.IsolatedStorage;
 12 using System.Text;
 13 
 14 namespace SLF5
 15 {
 16     /// <summary>
 17     /// 功能:SilverLight独立存储类
 18     /// 创建人:龚安川
 19     /// 创建时间:2012-08-18
 20     /// </summary>    
 21     public class SLCookie
 22     {
 23         /*当前系统Cookie 保存3个字段
 24         * PageAddress :记录最后一次访问的页面地址  
 25         * UserID: 用户名
 26         * ExitTime:退出时间  
 27         */
 28 
 29         #region 独立存储相关操作函数
 30 
 31         #region 设置Cookie
 32         /// <summary>
 33         /// 设置Cookie
 34         /// </summary>
 35         /// <param name="key">the cookie key</param>
 36         /// <param name="value">the cookie value</param>
 37         public static void SetCookie(string key, string value)
 38         {
 39             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 40             //判断该键是否存在
 41             if (settings.Contains(key))
 42             {
 43                 settings.Remove(key);
 44                 settings.Add(key, value);
 45             }
 46             else
 47             {
 48                 settings.Add(key, value);
 49             }
 50             settings.Save();
 51 
 52         }
 53         #endregion
 54 
 55         #region 读取一个已经存在的Cookie
 56         /// <summary>
 57         /// 读取一个已经存在的Cookie
 58         /// </summary>
 59         /// <param name="key">cookie key</param>
 60         /// <returns>null if the cookie does not exist, otherwise the cookie value</returns>
 61         public static string GetCookie(string key)
 62         {
 63             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 64             //判断该键是否存在
 65             if (settings.Contains(key))
 66             {
 67                 return settings[key] != null ? settings[key].ToString() : "";
 68             }
 69             else
 70             {
 71                 return string.Empty;
 72             }
 73         }
 74         #endregion
 75 
 76         #region   删除特定的Cookie(清空它的Value值)
 77         /// <summary>
 78         /// 删除特定的Cookie(清空它的Value值)
 79         /// </summary>
 80         /// <param name="key">the cookie key to delete</param>
 81 
 82         public static void DeleteCookie(string key)
 83         {
 84             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 85             //判断该键是否存在
 86             if (settings.Contains(key))
 87             {
 88                 settings.Remove(key);
 89             }
 90         }
 91         #endregion
 92 
 93         #region 判定指定的key-value对是否在cookie中存在
 94         public static bool Exists(String key, String value)
 95         {
 96             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 97             //判断该键是否存在
 98             if (settings.Contains(key))
 99             {
100                 return true;
101             }
102             else
103             {
104                 return false;
105             }
106         }
107         #endregion
108 
109         #region  获取当前cookie内容
110         public static string getCookieContent()
111         {
112             StringBuilder values = new StringBuilder();
113             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
114             if (settings != null && settings.Count > 0)
115             {
116                 foreach (string item in settings.Keys)
117                 {
118                     values.Append(settings[item] != null ? settings[item].ToString() : "");
119                 }
120                 return values.ToString();
121             }
122             else
123             {
124                 return string.Empty;
125             }
126         }
127         #endregion
128 
129         #region 清空本地cookie
130         public static void ClearCookies()
131         {
132             IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
133             settings.Clear();
134         }
135         #endregion 清空本地cookie
136 
137         #endregion
138     }
139 }

再为这个基类,写个辅助类,方便操作调用 SLCookieHelp.cs

View Code
 1 using System;
 2 using System.Net;
 3 using System.Windows;
 4 using System.Windows.Controls;
 5 using System.Windows.Documents;
 6 using System.Windows.Ink;
 7 using System.Windows.Input;
 8 using System.Windows.Media;
 9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 
12 namespace SLF5
13 {
14     /// <summary>
15     /// 功能:SilverLight的Cookie赋值类,用户获取历史信息
16     /// 创建人:龚安川
17     /// 创建时间:2012-08-18
18     /// </summary>  
19     public class SLCookieHelp
20     {
21         #region 似有字段
22         private string userid = "UserID";        
23         private string pageaddress = "PageAddress";
24         private string exittime = "ExitTime";
25        
26 
27         #endregion 似有字段
28 
29         #region 公共属性
30 
31         #region 登录账号
32         /// <summary>
33         /// 登陆名
34         /// </summary>
35         public string UserID
36         {
37             get
38             {
39                 return SLCookie.GetCookie(userid);
40             }
41             set
42             {
43                 SLCookie.SetCookie(userid, value);
44             }
45         }
46         #endregion 登录账号     
47 
48         #region 历史地址
49         /// <summary>
50         /// 历史地址
51         /// </summary>
52         public string PageAddress
53         {
54             get
55             {
56                 return SLCookie.GetCookie(pageaddress);
57             }
58             set
59             {
60                 SLCookie.SetCookie(pageaddress, value);
61             }
62         }
63 
64         #endregion 历史地址
65 
66         #region 退出时间
67         /// <summary>
68         /// 退出时间
69         /// </summary>
70         public string ExitTime
71         {
72             get
73             {
74                 return SLCookie.GetCookie(exittime);
75             }
76             set
77             {
78                 SLCookie.SetCookie(exittime, value);
79             }
80         }
81 
82         #endregion 历史地址
83 
84         #endregion 公共属性
85 
86         //清空COOKIE方法
87         public void ClearCookie()
88         {
89             SLCookie.ClearCookies();
90         }
91     }
92 }

接下来再APP.XAML.CS的启动事件增加判断,确定进入哪个界面,在退出事件里面修改本地存储登陆时间的更新。

一般情况下,在启动事件里面根据本地存储的数据判断启动不同的用户控件即可,但是本人这个项目正好用到了,基框架界面加载的方式,就顺便在这里也跟大家一起分享了。

现在的代码如下:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 
13 namespace SLF5
14 {
15     public partial class App : Application
16     {
17         //本地COOKIE操作类
18         SLCookieHelp slcookie = new SLCookieHelp();
19 
20         public App()
21         {
22             this.Startup += this.Application_Startup;
23             this.Exit += this.Application_Exit;
24             this.UnhandledException += this.Application_UnhandledException;
25 
26             InitializeComponent();
27         }
28 
29         private void Application_Startup(object sender, StartupEventArgs e)
30         {
31             //一般情况下根据本地存储的数据进行判断,指向RootVisual 启动不同的界面即可
32             //this.RootVisual = new MainPage();
33             //this.RootVisual = new Login();
34             //基框架界面加载方式
35             Host host = new Host();
36             this.RootVisual = host;
37 
38             //存储已经创建的容器
39             AppContext.Host = host;
40             AppContext.IsAdministrator = false;
41         }
42 
43         private void Application_Exit(object sender, EventArgs e)
44         {
45             slcookie.ExitTime = DateTime.Now.ToString();
46         }
47 
48         private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
49         {
50             // 如果应用程序是在调试器外运行的,则使用浏览器的
51             // 异常机制报告该异常。在 IE 上,将在状态栏中用一个 
52             // 黄色警报图标来显示该异常,而 Firefox 则会显示一个脚本错误。
53             if (!System.Diagnostics.Debugger.IsAttached)
54             {
55 
56                 // 注意: 这使应用程序可以在已引发异常但尚未处理该异常的情况下
57                 // 继续运行。 
58                 // 对于生产应用程序,此错误处理应替换为向网站报告错误
59                 // 并停止应用程序。
60                 e.Handled = true;
61                 Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
62             }
63         }
64 
65         private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
66         {
67             try
68             {
69                 string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
70                 errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
71 
72                 System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
73             }
74             catch (Exception)
75             {
76             }
77         }
78     }
79 }

基框架界面加载用到了以下几个类:

用户控件:Host.xaml 

XAML代码新建后不动即可,cs代码如下:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 
13 namespace SLF5
14 {
15     /// <summary>
16     /// 系统容器外壳,用于承载主业务界面(login;mainpage)
17     /// 加载顺序(RootVisual-Host-Login-MainPage)
18     /// </summary>
19     public partial class Host : UserControl,IHost
20     {
21         public Host()
22         {
23             InitializeComponent();
24             //默认设置为登录界面
25             SetChild(new Login());
26             Context.Host = this;
27         }
28 
29         private void SetChild(UserControl userControl)
30         {
31             LayoutRoot.Children.Clear();
32             LayoutRoot.Children.Add(userControl);
33         }
34 
35         public void SetRootVisual(UIElement content)
36         {
37             UserControl uc = content as UserControl;
38             if (uc == null)
39             {
40                 throw new Exception("Content is not a UserControl");
41             }
42             SetChild(uc);
43         }
44 
45         /// <summary>
46         /// 系统注销方法,注销后将返回登录界面
47         /// 注销动作由MainPage触发,将清空用户所有数据
48         /// </summary>
49         public void LoginOff()
50         {
51             AppContext.LogOff = false;
52             Context.Host.SetRootVisual(new Login());
53         }
54 
55     }
56 }

接口类:IHost.cs

View Code
 1 using System;
 2 using System.Net;
 3 using System.Windows;
 4 using System.Windows.Controls;
 5 using System.Windows.Documents;
 6 using System.Windows.Ink;
 7 using System.Windows.Input;
 8 using System.Windows.Media;
 9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 
12 namespace SLF5
13 {
14     public interface IHost
15     {
16         void SetRootVisual(UIElement content);
17         void LoginOff();
18     }
19 }

存储全局变量类:Context.cs 以及 AppContext.cs

View Code
 1 using System;
 2 using System.Net;
 3 using System.Windows;
 4 using System.Windows.Controls;
 5 using System.Windows.Documents;
 6 using System.Windows.Ink;
 7 using System.Windows.Input;
 8 using System.Windows.Media;
 9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 
12 namespace SLF5
13 {
14     /// <summary>
15     /// 上下文环境,存储全局共享数据
16     /// </summary>
17     public class Context
18     {
19         /// <summary>
20         /// 用于标识用户是否登录系统
21         /// </summary>
22         public static bool LoginFlag = false;
23 
24         public static IHost Host;
25     }
26 }
View Code
 1 using System;
 2 using System.Net;
 3 using System.Windows;
 4 using System.Windows.Controls;
 5 using System.Windows.Documents;
 6 using System.Windows.Ink;
 7 using System.Windows.Input;
 8 using System.Windows.Media;
 9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 
12 namespace SLF5
13 {
14     public static class AppContext
15     {
16         //当前UI容器
17         public static Host Host;
18 
19         //用户登录状态
20         public static bool LogOff = false;
21 
22         //判断用户的角色是否是管理员(默认管理员)
23         public static bool IsAdministrator = true;
24         // //数据菜单集合
25         //public static List<TreeViewModel> ListTreeData;
26 
27     }
28 }

接下来我们建立几个测试的页面:A.XAML,B.XAML.Default.XAML 以便多页面的切换,每个界面写一些自己标记的信息即可

登陆界面:Login.XAML

就是界面放2个LABEL和2个文本框,默认账号和密码都是:admin ,具体的项目当然要去查询数据库了。

重点来说MainPage界面的构造:

添加程序集:System.Windows.Controls.Navigation 的引用,在项目中添加引用,.NET下面就可以找到该程序集

主界面我利用GRID的2列,

左侧列放一个LISTBOX 来显示项目切换的页面地址信息(类似与菜单吧),

右侧列放置一个navigation 控件,利用此控件的IFRAM指向不同的界面信息。

XAML的源码如下:

View Code
<UserControl x:Class="SLF5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"          
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
        <!--左侧导航菜单-->
        <ListBox SelectionChanged="ListBox_SelectionChanged">
            <ListBoxItem Content="A页面">
                <ListBoxItem.ContentTemplate>
                    <DataTemplate x:Name="myTemplate">
                        <TextBlock Foreground="Blue" Text="{Binding}" />
                    </DataTemplate>
                </ListBoxItem.ContentTemplate>
            </ListBoxItem>          
            <ListBoxItem Content="B页面" ContentTemplate="{Binding ElementName=myTemplate}"></ListBoxItem>         
        </ListBox>
        <!--右侧显示区-->
        <Border Grid.Column="1" Grid.Row="2"  CornerRadius="4" Background="#CC166C9F"
                    BorderBrush="#99154270" BorderThickness="1">
            <navigation:Frame x:Name="Contnetframe" JournalOwnership="UsesParentJournal"/>
        </Border>
    </Grid>
</UserControl>

好了这样项目所需要的准备工作就已经做完,项目如下:

接下来开始我们的工作了,

在MainPage.Xaml.cs

定义本地存储辅助类

//本地cookie 类
SLCookieHelp slcookie = new SLCookieHelp();

增加界面加载事件  Loaded += new RoutedEventHandler(MainPage_Loaded);

View Code
1  string url = @"/Default.xaml";
2             if (!string.IsNullOrWhiteSpace(slcookie.PageAddress))
3             {
4                 url = slcookie.PageAddress;
5             }
6             Contnetframe.Navigate(new Uri(url, UriKind.Relative));

通过上面来加载显示的页面地址

然后在LISTBOX的选择事件,对本地存储的地址进行更新

View Code
 1       //选择项改变事件
 2         private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 3         {
 4             string url = @"/A.xaml";
 5             ListBox lst = sender as ListBox;           
 6             if (lst.SelectedItem == null)    return;
 7             ListBoxItem item = lst.SelectedItem as ListBoxItem;
 8             if (item.Content.ToString() == "B页面")
 9             {
10                 url = @"/B.xaml";
11             }     
12             //保存当前页面
13             slcookie.PageAddress = url;
14             //跳转连接
15             Contnetframe.Navigate(new Uri(url, UriKind.Relative));
16         }

这样主界面的工作就完成了,

最后再登陆界面的加载事件增加本地存储的判断以及登陆事件的本地存储账户信息的赋值即可

方法如下:

View Code
 1   //加载事件
 2         void Login_Loaded(object sender, RoutedEventArgs e)
 3         {
 4             //如果本地COOKIE有值 直接进入界面
 5             if (slcookie != null && !string.IsNullOrWhiteSpace(slcookie.UserID)  && !string.IsNullOrWhiteSpace(slcookie.ExitTime))
 6             {             
 7                 //判断退出时间和登陆时差是否大于5秒
 8                 DateTime now = DateTime.Now;
 9                 DateTime exit = Convert.ToDateTime(slcookie.ExitTime);
10                 TimeSpan ts = now - exit;
11                 //当操作时间小于5秒默认为刷新
12                 if (ts.TotalSeconds < 5 && ts.TotalSeconds > 0)
13                 {
14                     MainPage mainpage = new MainPage();
15                     Context.Host.SetRootVisual(mainpage);
16                 }
17                 else
18                 {
19                     //清空本地COOKIE
20                     slcookie.ClearCookie();
21                 }
22             }
23         }
View Code
 1  //登陆 
 2         private void btnLogin_Click(object sender, RoutedEventArgs e)
 3         {
 4             if (string.IsNullOrWhiteSpace(txtuid.Text) || string.IsNullOrWhiteSpace(txtpwd.Text))
 5             {
 6                 MessageBox.Show("用户名和密码不可为空");
 7                 return;
 8             }
 9 
10             if (txtuid.Text == "admin" && txtpwd.Text == "admin")
11             {
12                 slcookie.UserID = "admin";
13                 MainPage mainpage = new MainPage();
14                 Context.Host.SetRootVisual(mainpage);
15             }
16             else
17             {
18                 MessageBox.Show("用户名和密码输入不正确");
19             }
20         }

OK!完工,这样一个基本的SL程序的刷新功能就完成了,因为也是第一次做SL项目,当然肯定有考虑不周或者有更好的办法解决此问题,还我希望论坛的大侠们给予指导,如果写的不对的地方还望大家给予拍砖,虚心接受大家的评论。

运行效果如下:

Get Microsoft Silverlight

 源码地址:https://files.cnblogs.com/82767136/SLF5.rar

 

posted @ 2012-09-03 15:57  浪子の无悔  阅读(3785)  评论(0编辑  收藏  举报