silverlight – 保存变量wp7

什么是保存WP7等不同页面存储和可访问的变量(如userid)的最佳方法.

解决方法

有查询字符串方法,但实现起来很麻烦.

导航时,像HTTP查询字符串一样传递参数.

然后,在其他方面,检查密钥是否存在,并提取值.这样做的缺点是如果你需要做超过1,你需要自己键入它,它只支持字符串.

所以要传递一个整数,你需要转换它. (要传递一个复杂的对象,你需要把你需要的所有部分重新编译到另一边)

NavigationService.Navigate(new Uri("/PanoramaPage1.xaml?selected=item2",UriKind.Relative));

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        string selected = String.Empty;

        //check to see if the selected parameter was passed.
        if (NavigationContext.QueryString.ContainsKey("selected"))
        {
            //get the selected parameter off the query string from MainPage.
            selected = NavigationContext.QueryString["selected"];
        }

        //did the querystring indicate we should go to item2 instead of item1?
        if (selected == "item2")
        {
            //item2 is the second item,but 0 indexed. 
            myPanorama.DefaultItem = myPanorama.Items[1];
        }
        base.OnNavigatedTo(e);
    }

这是一个使用查询字符串的示例应用程序.
http://dl.dropbox.com/u/129101/Panorama_querystring.zip

更简单(更好)的想法是全局定义变量,或使用静态类.在App.xaml.cs中,定义

using System.Collections.Generic;

public static Dictionary<string,object> PageContext = new Dictionary<string,object>;

然后,在第一页上,简单地做

MyComplexObject obj;
int four = 4;
...

App.PageContext.Add("mycomplexobj",obj);
App.PageContext.Add("four",four);

然后,在新页面上,只需执行

MyComplexObj obj = App.PageContext["mycomplexobj"] as MyComplexObj;
int four = (int)App.PageContext["four"];

为安全起见,您应该检查对象是否存在:

if (App.PageContext.ContainsKey("four"))
int four = (int)App.PageContext["four"];

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...