[英]saving variables wp7
什么是保存WP7等不同頁面存儲和可訪問的變量(如userid)的最佳方法。
有查詢字符串方法,但實現起來很麻煩。
導航時,像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);
}
這是一個使用查詢字符串的示例應用程序。 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"];
您可以使用App級變量(在App.xaml.cs中定義)並從應用程序中的任何位置訪問它。 如果要保留,請將其推送到隔離存儲,並在應用程序啟動/激活時讀取它。 JSon可以使用幫助程序對隔離存儲的讀/寫進行序列化/反序列化。
查看Jeff的帖子( 這里 )有關使用獨立存儲的提示。
希望這可以幫助!
“最好”總是主觀的,但是,我認為應用程序服務是這類事情的一個很好的候選者: -
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
類中)。 它還提供了用於維持激活之間狀態的鈎子,這是必不可少的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.