转换列表到 ObservableCollection在WP7中
我不知道是否为时已晚或什么,但我不知道该怎么做...
我期望做什么,以及对象浏览器所说的,是这样的:
var oc = new ObservableCollection<T>( new List<T>() );
但是 ObservableCollection
有一个无参数构造函数。对象浏览器说有 2 个重载,其中 List 和 IEnuerable 应该能够传入。
我的设置有问题吗?手机版没有构造函数吗? (这会很奇怪)
如果这个真的不存在,那么现在使用 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>() );
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)
If this really doesn't exist, what is the standard way of doing this with WP7 now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
ObservableCollection 有几个构造函数,其输入参数为 List;或 IEnumerable:
列表list = new List();
ObservableCollection;集合 = new ObservableCollection(列表);
ObservableCollection has several constructors which have input parameter of List<T> or IEnumerable<T>:
List<T> list = new List<T>();
ObservableCollection<T> collection = new ObservableCollection<T>(list);
显然,您的项目针对的是 Windows Phone 7.0。不幸的是,接受
IEnumerable
或List
在 WP 7.0 中不可用,仅 无参数构造函数。其他构造函数在 Silverlight 4 及更高版本和 WP 7.1 及更高版本中可用,但在 WP 7.0 中不可用。我想您唯一的选择是获取列表并将项目单独添加到
ObservableCollection
的新实例中,因为没有现成的方法可以批量添加它们。但这并不能阻止您自己将其放入扩展或静态方法中。但是,如果没有必要,请不要这样做,如果您的目标框架提供重载,那么就使用它们。
Apparently, your project is targeting Windows Phone 7.0. Unfortunately the constructors that accept
IEnumerable<T>
orList<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.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.But don't do this if you don't have to, if you're targeting framework that provides the overloads, then use them.
将
List转换为list
到可观察集合,您可以使用以下代码:To convert
List<T> list
to observable collection you may use following code:您必须编写自己的扩展方法才能执行此操作:
You'll have to write your own extension method to do this:
此答案的扩展方法 IList; ObservableCollection 效果很好
Extension method from this answer IList<T> to ObservableCollection<T> works pretty well
使用这个:
Use this:
如果您要添加大量项目,请考虑从 ObservableCollection 派生您自己的类并将项目添加到受保护的 Items 成员中 - 这不会在观察者中引发事件。完成后,您可以引发适当的事件:
将许多项添加到已绑定到 UI 元素(例如 LongListSelector)的 ObservableCollection 时,这可能会带来巨大的性能差异。
在添加项目之前,您还可以确保有足够的空间,以便通过在 BulkObservableCollection 类中实现此方法并在调用 AddRange 之前调用它来不断扩展列表:
If you are going to be adding lots of items, consider deriving your own class from ObservableCollection and adding items to the protected Items member - this won't raise events in observers. When you are done you can raise the appropriate events:
When adding many items to an ObservableCollection that is already bound to a UI element (such as LongListSelector) this can make a massive performance difference.
Prior to adding the items, you could also ensure you have enough space, so that the list isn't continually being expanded by implementing this method in the BulkObservableCollection class and calling it prior to calling AddRange:
我做了一个扩展,所以现在我可以通过执行以下操作来加载带有列表的集合:
扩展名是:
I made an extension so now I can just load a collection with a list by doing:
The extension is:
Zin Min提供的答案用一行代码解决了我的问题。出色的!
我在将通用列表转换为通用 ObservableCollection 时遇到了同样的问题,以使用列表中的值来填充通过 WPF 窗口的工厂类参与绑定的组合框。
_expediteStatuses = new ObservableCollection(_db.getExpediteStatuses());
以下是 getExpediteStatuses 方法的签名:
public List; getExpediteStatuses()
The answer provided by Zin Min solved my problem with a single line of code. Excellent!
I was having the same issue of converting a generic List to a generic ObservableCollection to use the values from my List to populate a ComboBox that is participating in binding via a factory class for a WPF Window.
_expediteStatuses = new ObservableCollection<ExpediteStatus>(_db.getExpediteStatuses());
Here is the signature for the getExpediteStatuses method:
public List<ExpediteStatus> getExpediteStatuses()