Windows Phone 7 Silverlight 使用会话

发布于 2024-11-07 05:00:57 字数 222 浏览 0 评论 0原文

我正在创建一个 Windows 7 移动 Silverlight 项目。我使用 Rest api 使用 Authentication 类进行身份验证。我收到一个身份验证令牌作为响应,并将其分配给类属性 AuthToken,因为我需要在不同的地方使用它。有什么方法可以将此 AuthToken 存储在会话或其他任何东西中。因为我在wp7中没有找到任何会话示例。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还在原地等你 2024-11-14 05:00:57

如果您需要临时会话存储(应用程序的生命周期,包括用户使用后退按钮返回应用程序时),那么您可以使用电话状态。电话状态类似于 ASP.NET 中的会话状态。它只是带有字符串键的(可序列化)对象的字典,并且不会在应用程序启动时进行维护,但当您的应用程序从返回堆栈导航到时,它会被恢复。

下面是一个用于恢复名为 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();
    }
}

如果您需要在应用程序的所有实例中存储设置,请查看 IsolatedStorageSettings 非常适合此操作。根据您的需求,还有其他选项(Charles Petzold 有一本免费电子书,其中包含一些很棒的示例)。


不知道为什么上面的代码不适合您,但另一种选择是使用使用isolatedStorageSettings保存的应用程序属性。下面是一个示例:

在您的 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;
    }

然后您可以使用以下代码在应用程序中的任何位置访问此属性:

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

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));
    }
不甘平庸 2024-11-14 05:00:57

您没有找到任何会话示例,因为据我所知,WP7 没有会话。您应该能够使用独立存储来保存您的 AuthToken。但请记住,它不会像您在会话中预期的那样在一定时间后过期。

请参阅以下内容或 google 搜索独立存储以获取示例:
http://www.windowsphonegeek.com /tips/all-about-wp7-isolated-storage-store-data-in-isolatedstoragesettings

希望有帮助。我没有做过大量的WP7开发,但我熟悉Silverlight。

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.

征棹 2024-11-14 05:00:57

假设您只想在当前应用程序实例的生命周期内使用它:

最简单的答案是将其存储为静态属性而不是实例属性。

简单的答案是将其存储为应用程序类 (App.xaml.cs) 的属性。然后您可以使用 ((App)(Application.Current)).Token 在任何地方访问它。

不太简单但可能更好的答案是将其存储在 ViewModel 属性中,然后您可以通过 ViewModel 定位器访问它 - 看一下MVVM Light 为例。

在所有这些情况下,您都需要考虑逻辑删除,以便在用户单击“开始”然后单击“返回”(例如)时恢复它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文