繁体   English   中英

保存变量wp7

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM