Windows Phone 7 Silverlight 使用会话
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要临时会话存储(应用程序的生命周期,包括用户使用后退按钮返回应用程序时),那么您可以使用电话状态。电话状态类似于 ASP.NET 中的会话状态。它只是带有字符串键的(可序列化)对象的字典,并且不会在应用程序启动时进行维护,但当您的应用程序从返回堆栈导航到时,它会被恢复。
下面是一个用于恢复名为 myObject 的自定义对象的示例:
如果您需要在应用程序的所有实例中存储设置,请查看 IsolatedStorageSettings 非常适合此操作。根据您的需求,还有其他选项(Charles Petzold 有一本免费电子书,其中包含一些很棒的示例)。
不知道为什么上面的代码不适合您,但另一种选择是使用使用isolatedStorageSettings保存的应用程序属性。下面是一个示例:
在您的 App.xaml.cs 中:
然后您可以使用以下代码在应用程序中的任何位置访问此属性:
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:
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:
You can then access this property anywhere in your application by using this code:
您没有找到任何会话示例,因为据我所知,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.
假设您只想在当前应用程序实例的生命周期内使用它:
最简单的答案是将其存储为静态属性而不是实例属性。
简单的答案是将其存储为应用程序类 (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).