什么是保存变量的最佳方式,如在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. 保存变量名
- 3. 保存变量(Android)
- 4. 保存Java变量
- 5. 保存增量int变量
- 6. 无法保存wp7的IsolatedStorageSettings.ApplicationSettings
- 7. 公共变量wp7芒果
- 8. 保存范围变量
- 9. Greenrobot Eventbus保存变量
- 10. 保存新的变量值
- 11. 在AnyLogic中保存变量
- 12. 变量需要保存
- 13. 保存编号变量
- 14. 保存会话变量
- 15. 保存环境变量
- 16. 在Python中保存变量
- 17. 保存tensorflow一些变量
- 18. $ _SESSION变量不保存
- 19. 保存os.walk()在变量
- 20. 保存变量的地址
- 21. 保存到类变量
- 22. Keras保存中间变量
- 23. PHP保存会话变量
- 24. 将变量保存到CharField
- 25. json保存到变量
- 26. 保存输出到变量
- 27. 保存缓存到一个变量
- 28. 将图像保存到手机? WP7 C#
- 29. 如何在wp7中保存WebBrowser页面?
- 30. 的Net :: FTP getbinaryfile()保存到文件VS保存到变量
thnx对于那真的很好的帮助:) – Arnstein 2011-03-12 19:19:35
+1。对于静态字典的使用,因为它使得它更容易处理执行模型,因为'Activiated'和'Deactivated'事件只需要将这个字典存储在'State'中。然而,如果明确说明这个答案以及只存储可串行化对象的警告,这个答案将会得到改进。 – AnthonyWJones 2011-03-12 20:30:14