如何存储和使用应用程序范围的对象 Windows Phone 7/8? [英] how to store and use application wide object windows phone 7/8?

查看:25
本文介绍了如何存储和使用应用程序范围的对象 Windows Phone 7/8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我使用了几乎所有的 Windows Phone 页面,目前我通过

I have an object which i am using almost all windows phone pages, currently i am using via

PhoneApplicationService.Current.State["xx"] = m_xx;
NavigationService.Navigate(new Uri("/abc.xaml", UriKind.Relative));

但这必须对我不想要的所有活动进行.有没有更好的方法来保存我可以在所有页面中使用的 m_xx 对象?最佳做法是什么?

but this has to be done for all activities, which i don't want. is there a better way to save m_xx object which i can use in all the pages? what is the best practice?

我可以将对象静态化到某个类,然后通过该类名跨页面使用吗?

Can i make object static to some class and then use across pages via that class name?

推荐答案

您可能想要研究 MVVM 模式.

You might want to look into the MVVM pattern.

不过,如果您的应用程序有太多变化,您可以使用混合方法,将共享上下文存储在静态属性中.

Still, if it's too much a change for your application, you can use a hybrid approach, with a shared context stored in a static property.

首先,创建一个 Context 类并将您的共享属性放入其中:

First, create a Context class and put your shared properties inside:

public class Context
{
    public string SomeSharedProperty { get; set; }
}

然后,在 App.xaml.cs 中,创建一个静态属性来存储上下文:

Then, in the App.xaml.cs, create a static property to store the context:

private static Context context;

public static Context Context
{
    get
    {
        if (context == null)
        {
            context = new Context();
        }

        return context;
    }
}

然后,您可以从应用程序的任何位置访问上下文以存储/检索数据:

Then, from anywhere in your application, you can access your context to store/retrieve data:

App.Context.SomeSharedProperty = "Hello world!";

这篇关于如何存储和使用应用程序范围的对象 Windows Phone 7/8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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