頁面切換時傳遞List

摘要:頁面切換時傳遞List

有時在頁面切換的時候會想要傳遞List過去,之前不知道怎麼弄,只好用個全域變數來傳,最近終於找到方法了!

要傳遞的那個頁面要寫成

List< string > list = new List< string >(); 
list.Add("WP7"); 
list.Add ("WM6.5"); 
PhoneApplicationService.Current.State.Add("mobile",list); 
this.NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));

接受傳遞那方要這樣做

if (PhoneApplicationService.Current.State.ContainsKey("mobile")) 
{ 
  List < string > list=PhoneApplicationService.Current.State["mobile"] as List < string >; 
  blahblah...
}

以上code來自 http://www.dotblogs.com.tw/kylin/archive/2010/09/08/17608.aspx

 

嗯...取完之後不知道要不要做remove的動作就是,目前我是會在最後加上

PhoneApplicationService.Current.State.Remove("mobile");

不知道會不會比較省資源就是,anyway,反正就放著吧。