保存像userid这样的变量的最好方法是什么,这些变量存储在WP7的不同页面中,并且可以从不同的页面访问。
发布于 2011-03-13 02:12:13
有querystring方法,但实现起来可能有点麻烦。
导航时,像传递HTTP查询字符串一样传递参数。
然后,在另一端,检查键是否存在,并提取值。这样做的缺点是,如果你需要做多于1的操作,你需要自己输入它,而且它只支持字符串。
所以要传递一个整数,你需要对它进行转换。(要传递一个复杂的对象,您需要获取在另一端重新编译它所需的所有片段)
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);
}这是一个使用querystring的示例应用程序。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"];发布于 2011-03-13 01:26:32
您可以使用应用程序级别变量(在App.xaml.cs中定义),并从应用程序中的任何位置访问它。如果你想坚持下去,把它放到独立存储中,然后在App启动/激活时读取它。有一些帮助器可用于JSon序列化/反序列化独立存储中的读/写。
请查看Jeff的帖子(here)关于使用独立存储的技巧。
希望这能有所帮助!
发布于 2011-03-13 04:24:17
“最好的”总是主观的,然而,我认为应用程序服务是这类事情的一个很好的候选者:
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());
}
}
}你现在可以在你的应用程序中的任何地方访问你的UserID:
AuthenticationService.Current.UserID此通用模式可用于维护关键应用程序范围的服务的分离(不需要将一大堆不相关的属性加载到App类中)。它还提供了用于在激活之间维护状态的钩子,这是必不可少的。
https://stackoverflow.com/questions/5283565
复制相似问题