2011-03-12 70 views

回答

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"]; 
+0

thnx对于那真的很好的帮助:) – Arnstein 2011-03-12 19:19:35

+1

+1。对于静态字典的使用,因为它使得它更容易处理执行模型,因为'Activiated'和'Deactivated'事件只需要将这个字典存储在'State'中。然而,如果明确说明这个答案以及只存储可串行化对象的警告,这个答案将会得到改进。 – AnthonyWJones 2011-03-12 20:30:14

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类中)。它还提供了必要的激活之间维持状态的挂钩。