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

末鹿安然 提交于 2019-12-10 11:45:38

问题


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));

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?


回答1:


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!";


来源:https://stackoverflow.com/questions/18671117/how-to-store-and-use-application-wide-object-windows-phone-7-8

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