如何存储和使用应用程序范围的对象 Windows Phone 7/8?

编程入门 行业动态 更新时间:2024-10-23 12:31:04
本文介绍了如何存储和使用应用程序范围的对象 Windows Phone 7/8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个对象,我使用了几乎所有的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 14:12:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   对象   Phone   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!