Convert List to ObservableCollection in WP7

后端 未结 10 908
不知归路
不知归路 2020-12-25 10:10

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:

10条回答
  •  温柔的废话
    2020-12-25 10:20

    Apparently, your project is targeting Windows Phone 7.0. Unfortunately the constructors that accept IEnumerable or List 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.

    var list = new List { /* ... */ };
    var oc = new ObservableCollection();
    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.

提交回复
热议问题