Wp7 persistent variables

What is the best way to save variables, such as userid, that are stored and accessible from different pages in WP7.

+3
source share
3 answers

There is a querystring method, but it can be kind of a pain to implement.

When navigating, execute a parameter similar to HTTP requests.

Then, on the other hand, check if the key exists and retrieve the value. The disadvantage of this is that if you need to do more than 1, you need to enter it into yourself, and it only supports strings.

So, to pass an integer, you need to convert it. (And in order to convey a complex object, you need to take all the parts that you need to recompile on the other side)

NavigationService.Navigate(new Uri("/PanoramaPage1.xaml?selected=item2", UriKind.Relative));

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        string selected = String.Empty;

        //check to see if the selected parameter was passed.
        if (NavigationContext.QueryString.ContainsKey("selected"))
        {
            //get the selected parameter off the query string from MainPage.
            selected = NavigationContext.QueryString["selected"];
        }

        //did the querystring indicate we should go to item2 instead of item1?
        if (selected == "item2")
        {
            //item2 is the second item, but 0 indexed. 
            myPanorama.DefaultItem = myPanorama.Items[1];
        }
        base.OnNavigatedTo(e);
    }

, . http://dl.dropbox.com/u/129101/Panorama_querystring.zip

( ) , . App.xaml.cs

using System.Collections.Generic;

public static Dictionary<string,object> PageContext = new Dictionary<string,object>;

MyComplexObject obj;
int four = 4;
...

App.PageContext.Add("mycomplexobj",obj);
App.PageContext.Add("four",four);

MyComplexObj obj = App.PageContext["mycomplexobj"] as MyComplexObj;
int four = (int)App.PageContext["four"];

, , , , :

if (App.PageContext.ContainsKey("four"))
int four = (int)App.PageContext["four"];
+5

App ( App.xaml.cs) . , / . JSon , / / .

Jeff post () .

, !

+1

, "" , , , - : -

public interface IPhoneApplicationService : IApplicationService
{
     string Name {get; set;}
     object Deactivating();
     void Activating(object state);
}

public class AuthenticationService : IPhoneApplicationService
{
    public static AuthenticationService Current {get; private set; }

    public void StartService(ApplicationServiceContext context)
    {
        Current = this;
    }

    public void StopService()
    {
        Current = null;
    }

    public string Name {get; set;}

    public object Deactivating()
    {
        // Return an serialisable object such as a Dictionary if necessary.
        return UserID;
    }

    public void Activating(object state)
    {
        UserID = (int)state;
    }

    public int UserID { get; private set; }

    public void Logon(string username, string password)
    {
        // Code here that eventually assigns to UserID.
    }
}

App.xaml: -

<Application.ApplicationLifetimeObjects>
    <!--Required object that handles lifetime events for the application-->

    <shell:PhoneApplicationService 
        Launching="Application_Launching" Closing="Application_Closing" 
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>

    <local:AuthenticationService Name="AuthServ" />

</Application.ApplicationLifetimeObjects>

App.xaml.cs: -

    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
        var state = PhoneApplicationService.Current.State;
        foreach (var service in ApplicationLifetimeObjects.OfType<IPhoneApplicationService>())
        {
            if (state.ContainsKey(service.Name))
            {
                service.Activating(state[service.Name]);
            }
        }
    }

    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        var state = PhoneApplicationService.Current.State;
        foreach (var service in ApplicationLifetimeObjects.OfType<IPhoneApplicationService>())
        {
            if (state.ContainsKey(service.Name))
            {
                state[service.Name] = service.Deactivating();
            }
            else
            {
                state.Add(service.Name, service.Deactivating());
            }
        }
    }

UserID : -

 AuthenticationService.Current.UserID

( , , App). , .

+1

All Articles