通用单例并在页面之间共享数据 [英] Generic Singleton and share data between pages

查看:31
本文介绍了通用单例并在页面之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在我的 windows phone 8 应用程序中的页面之间共享数据(复杂数据),我想实现一个单例,但我希望它是通用的,这可能吗?我想它为每种类型创建了一个新实例,不是吗?

To share data (complexe data ) between pages in my windows phone 8 application I want to implement a singleton, but I want it to be generic, is it possible? I suppose that it creates a new instance for each type isn't it?

 public sealed class NavigationContextService<T>
{
    private static readonly NavigationContextService<T> instance = new NavigationContextService<T>();
    private NavigationContextService()
    {
    }
     public static NavigationContextService<T> Instance
    {
        get
        {
            return instance;
        }
    }

    public List<T> ShareList { get; set; }
    public T ShareData { get; set; }
}

推荐答案

它正在为每个类型创建一个新实例,因为它是泛型的——你希望它是这样的(如果你从泛型开始,看看一些教程、博客或 MSDN - 您可以在互联网上轻松找到许多).

It is creating a new instance for every type, because it is generic - you want it to be like this (if you start with generics, take a look at some tutorials, blogs or MSDN - you will easily find many in the internet).

它仍然是一个单例.当你使用

It is still a singleton. When you use

NavigationContextService<string>.Instance.ShareList.Add("Text");

然后你有一个 string 类型的实例.当您想要创建不同类型的相同方法/类时,泛型会很有帮助.

then you have one Instance for type string. Generics helps a lot when you want to create same methods/classes that differ in type.

另一方面,如果您想创建包含不同类型的单例,那么您可以例如将您的类修改为非泛型,如下所示:

On the other hand if you want to create Singleton that will hold different types then you can for example modify your class to be non Generic like this:

public sealed class NavigationContextServiceNonGeneric
{
    private static readonly NavigationContextServiceNonGeneric instance = new NavigationContextServiceNonGeneric();
    private NavigationContextServiceNonGeneric() { ShareList = new List<object>(); }

    public static NavigationContextServiceNonGeneric Instance
    { get { return instance; } }

    public List<object> ShareList { get; set; }
    public object ShareData { get; set; }
}

正如你在上面的代码中看到的,我没有定义共享数据的确切"类型——它是 object 类型.然后,您可以轻松地使用它保存大部分数据:

As you can see in the code above I haven't defined the 'exact' type of shared data - it is object type. Then you can easily hold most of data with it:

NavigationContextServiceNonGeneric.Instance.ShareList.Add("Text");
NavigationContextServiceNonGeneric.Instance.ShareList.Add(3);
NavigationContextServiceNonGeneric.Instance.ShareList.Add(3.0f);

是单例,可以保存不同类型的共享数据.但是它也有缺点 - 主要是您必须记住您持有的数据类型和顺序.由于这个事实,我认为通用版本更好.

It is singleton, which can hold different types of shared data. BUT it has also disavantages - the main is that you have to remember what type of data you hold and in what order. In my opinion Generic version is better because of that fact.

一切都取决于您的代码的目的.这两种方法可能有更简单更好的方法.

Everything depends on the purpose of your code. There may be easier and better ways that those two approaches.

至于页面导航,例如您可以尝试使用 这篇文章 - 你扩展导航服务来传递对象:

As for the Page Navigation, you can for example try to use a method from this article - you extend Navigation service to pass the object:

public static class Extensions
{
  private static object Data;

  public static void Navigate(this NavigationService navigationService, Uri source, object data)
  {
     Data = data;
     navigationService.Navigate(source);
  }

  public static object GetNavigationData(this NavigationService service) { return Data; }
}

然后你使用它:

NavigationService.Navigate(yourUri, DataToPass);

导航后,您可以获得数据:

After Navigation you can get your data:

string myTextData = NavigationService.GetNavigationData() as string;

这种方法有缺点:它不是类型安全的,并且您的数据不会在 Tombstone 模式下保留.

This method has to disadvantages: it is not type-safe and your data won't be preserved in Tombstone mode.

至于第二个缺点,您可以轻松使用 PhoneApplicationService.State 属性 用于页面导航 - 它是一个字典(在墓碑删除时保留):

As for the second disadvantage you can easily use PhoneApplicationService.State Property for the purpose of Page Navigation - it is a dictionary (which is preserved while tombstoning):

PhoneApplicationService.Current.State.Add("data", yourData);

然后当您想要获取数据时:

Then when you want to get your data:

yourDataType yourData = PhoneApplicationService.Current.State["data"] as yourDataType;

您还可以通过更多方式传递数据.

There are also more ways in which you can pass the data.

这篇关于通用单例并在页面之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆