什么是保存变量的最佳方式,如在WP7的不同页面存储和访问的userid。保存变量wp7
Q
保存变量wp7
3
A
回答
5
有querystring方法,但可能是一种痛苦实施。
导航时,像HTTP查询字符串一样传递参数。
然后,在其他方面,检查密钥是否存在,并提取该值。这个缺点是如果你需要做的不止一个,你需要自己输入,而且它只支持字符串。
所以要传递一个整数,你需要转换它。 (并通过一个复杂的对象,你需要考虑你需要重新编译它在另一边的作品)
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"];
1
您可以使用应用程序级变量(在App.xaml.cs中定义),并可以在应用程序中的任何位置访问它。如果您想坚持,请将其推入独立存储并在应用程序启动/激活时阅读。 JSon有可用的助手序列化/反序列化从独立存储中读取/写入数据。
查看Jeff的帖子(here)了解使用隔离存储的提示。
希望这会有所帮助!
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());
}
}
}
您现在可以访问你在与你的应用的任何用户名: -
AuthenticationService.Current.UserID
此一般模式可用于维护关键应用程序范围服务的分离(您不会将一大堆不协调的属性加载到您的App
类中)。它还提供了必要的激活之间维持状态的挂钩。
相关问题
- 1. WP7是否保存变量关闭?
- 2. WP7墓碑内存保存
- 3. 保存变量值
- 4. 保存Java变量
- 5. 保存变量名
- 6. Java变量保存
- 7. 保存变量(Android)
- 8. 保存增量int变量
- 9. 保存大量变量
- 10. 无法保存wp7的IsolatedStorageSettings.ApplicationSettings
- 11. 保存群集变量/变量PSPP
- 12. 公共变量wp7芒果
- 13. WP7 - 访问私有变量
- 14. 保存tensorflow一些变量
- 15. json保存到变量
- 16. My.Settings不保存变量
- 17. 保存到类变量
- 18. Arduino保存http变量
- 19. Javascript Blob保存到变量
- 20. 变量需要保存
- 21. 保存编号变量
- 22. 保存变量在黄瓜?
- 23. 保存和撤回变量
- 24. 保存和加载变量
- 25. 在Python中保存变量
- 26. Matlab变量永久保存
- 27. 保存在变量jQuery的
- 28. 保存范围变量
- 29. Greenrobot Eventbus保存变量
- 30. 在Android中保存变量
thnx对于那真的很好的帮助:) – Arnstein 2011-03-12 19:19:35
+1。对于静态字典的使用,因为它使得它更容易处理执行模型,因为'Activiated'和'Deactivated'事件只需要将这个字典存储在'State'中。然而,如果明确说明这个答案以及只存储可串行化对象的警告,这个答案将会得到改进。 – AnthonyWJones 2011-03-12 20:30:14