使用独立存储的IsolatedStorageSettings对象进行页面间传值

来源:互联网 发布:新浪微博个性域名大全 编辑:程序博客网 时间:2024/06/03 16:21

    在asp.net开发中页面间的传值方法有很多种,如Session、Cookie等技术,但是在Silverlight中,我们可以通过IsolatedStorageSettings这个独立存储对象来实现。

 

    (1)、首先需要在每个需要使用传递过来的值的页面中添加命名空间:using System.IO.IsolatedStorage;

 

    (2)、如在Login.xaml页面中,首先创建一个IsolatedStorage对象,如下:

           //使用独立存储的IsolatedStorageSettings对象
           private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

 

     (3)、在登录按钮事件中,验证通过并跳转的语句后面添加如下类似于Session的用于保存用户信息,并可以在其他页面中读取该值。

            ...

            if (!appSettings.Contains("uname"))
           {
                appSettings.Add("uname", this.tbUserName.Text.Trim());
           }

           ...

    (4)、在MainPage.xaml页面中,首先创建一个IsolatedStorageSettings对象,然后利用此对象来读取保存的用户信息。

           ...

          //使用独立存储的IsolatedStorageSettings对象
           private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

           ...

           if(appSettings.Contains("uname"))

          {          

                this.tbUserName.Text = appSettings["uname"].ToString();

           }