Windows Phone 7 Silverlight using session

2019-02-11 08:29发布

问题:

I am creating a Windows 7 mobile Silverlight project. I use Rest api for authentication using a class say Authentication. I get an authentication token as a response and I assign it to a class property AuthToken because I need to use this in different places. Is there any way to store this AuthToken in session or any thing else. Because I did not find any session example in wp7. Thanks

回答1:

You're not finding any session examples because WP7 doesn't have session as far as I know. You should be able to use Isolated Storage to keep your AuthToken around. Bear in mind, however, that it wont expire after a certain amount of time like you'd expect with a session.

See the following or google search Isolated Storage for examples: http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage-store-data-in-isolatedstoragesettings

Hope it helps. I haven't done a great deal of WP7 development, but I'm familiar with Silverlight.



回答2:

If you want temporary session storage (the life of the app including when a user uses the back button to return to your app) then you can use Phone State. The Phone State is similar to Session State in ASP.NET. It is just a dictionary of (serializable) objects with string keys and is not maintained across launches of your app, but it is restored when your app is navigated to from the Back Stack.

Here is an example of it's use to restore some custom object named myObject:

private CustomObject myObject;

protected override void OnNavigatedFrom(NavigationEventArgs args)
{
    //Save to State when leaving the page
    PhoneApplicationService.Current.State["myObject"] = myObject;
    base.OnNavigatedFrom(args);
}

protected override void OnNavigatedTo(NavigationEventArgs args)
{
    if (PhoneApplicationService.Current.State.ContainsKey("myObject"))
    {
        //Restore from State
        myObject = (CustomObject)PhoneApplicationService.Current.State["myObject"];
    }
    else
    {
        //No previous object, so perform initialization
        myObject = new myObject();
    }
}

If you need to store settings across all instances of your app then look into IsolatedStorageSettings which is perfect for this. There are other options depending on your needs (Charles Petzold has a free eBook with some great examples).


Not sure why the above code didn't work for you, but another option is to use an app property which is saved using IsolatedStorageSettings. Here is an example:

In your App.xaml.cs:

    public bool VibrationOn { get; set; }

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        LoadSettings();
    }

    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
        LoadSettings();
    }

    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        SaveSettings();
    }

    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        SaveSettings();
    }

    private void LoadSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        bool vo;
        if (settings.TryGetValue<bool>("VibrationOn", out vo))
            VibrationOn = vo;
        else
            VibrationOn = true;
    }

    private void SaveSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        settings["VibrationOn"] = VibrationOn;
    }

You can then access this property anywhere in your application by using this code:

    if (Application.Current.VibrationOn)
    {
        VibrateController.Default.Start(TimeSpan.FromMilliseconds(200));
    }


回答3:

Assuming you only want it for the lifetime of the current application instance:

The simplest answer is to store it as a static property instead of an instance property.

The simple answer is to store it as a property of your Application class (App.xaml.cs). Then you can access it anywhere using ((App)(Application.Current)).Token

The less simple, but probably better answer would be store it in a ViewModel property, which you could then access via a ViewModel locator - take a look at MVVM Light for examples.

In all of these cases you'll need to take into account Tombstoning, to restore it if the user hits Start and then Back (for example).