转换列表< T>到ObservableCollection< T>在WP7中 [英] Convert List<T> to ObservableCollection<T> in WP7

查看:96
本文介绍了转换列表< 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...

我期望什么

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

但是 ObservableCollection< T> 有一个无参数构造函数。对象浏览器说应该有两个重载可以传递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。不幸的是,接受 IEnumerable< T> List< T> 不可用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.

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

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