保存变量 wp7 [英] saving variables wp7

查看:34
本文介绍了保存变量 wp7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WP7 中保存可从不同页面访问的用户 ID 等变量的最佳方法是什么.

Whats the best way to save variables like userid that is stored and reachable from different pages in WP7.

推荐答案

有 querystring 方法,但实现起来可能有点麻烦.

There's the querystring method, but can be kind of a pain to implement.

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

When navigating, pass the parameter like a HTTP querystring.

然后,在另一边,检查密钥是否存在,并提取值.这样做的缺点是如果你需要做的超过1个,你需要自己输入,而且它只支持字符串.

Then, on the otherside, check if the key exists, and extract the value. The downside of this is if you need to do more than 1, you need to type it in yourself, and it only supports strings.

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

So to pass an integer, you'd need to convert it. (And to pass a complex object, you need to take all the pieces you need to recompile it on the other side)

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

Here's a sample app that uses a querystring. http://dl.dropbox.com/u/129101/Panorama_querystring.zip

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

A easier (and better) idea is to define a variable globally, or use a static class. In App.xaml.cs, define

using System.Collections.Generic;

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

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

Then, on the first page, simply do

MyComplexObject obj;
int four = 4;
...

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

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

Then, on the new page, simply do

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

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

To be safe, you should probably check if the object exists:

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

这篇关于保存变量 wp7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆