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

932 Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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