how to store and use application wide object windows phone 7/8?

折月煮酒 提交于 2019-12-08 04:15:30

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.

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

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

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!";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!