转换列表<T>到 ObservableCollection<T>在 WP7 [英] Convert List&lt;T&gt; to ObservableCollection&lt;T&gt; in WP7

查看:15
本文介绍了转换列表<T>到 ObservableCollection<T>在 WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是不是太晚了还是什么,但我不知道该怎么做...

I don't know if it's just too late or what, but I don't see how to do this...

我期望做的,以及对象浏览器所说的,是这样的:

What I'm expecting to do, and what the object browser says is there, is this:

var oc = new ObservableCollection<T>( new List<T>() );

但是 ObservableCollection 有一个无参数的构造函数.对象浏览器说有 2 个重载,其中 List 和 IEnuerable 应该能够被传入.

But ObservableCollection<T> has a single parameterless constructor. The object browser says there is 2 overloads where List and IEnuerable should be able to be passed in.

我的设置有问题吗?构造函数不是手机版本吗?(那会很奇怪)

Is there something wrong with my setup? Are the constructors not on the phone version? (that would be strange)

如果这真的不存在,那么现在使用 WP7 执行此操作的标准方法是什么?

If this really doesn't exist, what is the standard way of doing this with WP7 now?

推荐答案

显然,您的项目面向 Windows Phone 7.0.不幸的是,接受 IEnumerableList 在 WP 7.0 中不可用,只有 无参数构造函数.其他构造函数在 Silverlight 4 及更高版本和 WP 7.1 及更高版本中可用,只是在 WP 7.0 中不可用.

Apparently, your project is targeting Windows Phone 7.0. Unfortunately the constructors that accept IEnumerable<T> or List<T> are not available in WP 7.0, only the parameterless constructor. The other constructors are available in Silverlight 4 and above and WP 7.1 and above, just not in WP 7.0.

我想您唯一的选择是获取您的列表并将项目单独添加到 ObservableCollection 的新实例中,因为没有现成的方法可以批量添加它们.尽管这并不是要阻止您自己将其放入扩展或静态方法中.

I guess your only option is to take your list and add the items into a new instance of an ObservableCollection individually as there are no readily available methods to add them in bulk. Though that's not to stop you from putting this into an extension or static method yourself.

var list = new List<SomeType> { /* ... */ };
var oc = new ObservableCollection<SomeType>();
foreach (var item in list)
    oc.Add(item);

但如果您没有必要,请不要这样做,如果您的目标是提供重载的框架,请使用它们.

But don't do this if you don't have to, if you're targeting framework that provides the overloads, then use them.

这篇关于转换列表<T>到 ObservableCollection<T>在 WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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