破解 Silverlight NotifyCollectionChangedEventArgs 以支持多个项目有什么用吗?
NotifyCollectionChangedEventArgs 的 Silverlight 版本与完整框架版本不同,因为它不接受多个(添加、更改、删除)项目。采用列表的构造函数实际上丢失了,因此微软似乎打算阻止这种用法。事实上,如果您传入一个项目集合,它们只是嵌套为内部集合的第一个项目。
然而!由于 NewItems 和 OldItems 成员是 IList 类型,因此它们不是不可变的并且可以增长。我制作了以下助手来测试这个想法:
private NotifyCollectionChangedEventArgs CreateEventArgsWithMultiple(NotifyCollectionChangedAction action, IEnumerable items, int newStartingIndex)
{
NotifyCollectionChangedEventArgs eventArgs = null;
foreach (var item in items)
{
if (eventArgs == null)
{
eventArgs = new NotifyCollectionChangedEventArgs(action, item, newStartingIndex);
}
else
{
eventArgs.NewItems.Add(item);
}
}
return eventArgs;
}
我还没有看到任何问题,但我正在寻找有关 Silverlight 的这个特定角落的经验和意见。我应该像这样批量添加,还是只使用重置?
顺便说一下,这是在 Windows Phone 7.1 (Mango) 上。
编辑:跟进埃尔诺的评论。微软在 中表示MSDN 上的这个(措辞不佳的)Silverlight 文档页面 可以“一般”假设 NewItems 只有一个元素,甚至建议使用的快捷方式NewItems[0] 来访问它。因此,他们保留了 IList 签名以实现“兼容性”,但随后又继续破坏类型的含义。令人失望。
The Silverlight version of NotifyCollectionChangedEventArgs differs from the full framework version, in that it does not accept multiple (added, changed, removed) items. The constructors that take lists are in fact missing, so it appears Microsoft intended to block this usage. Indeed, if you pass in a collection of items, they're just nested as the first item of an internal collection.
However! Since the NewItems and OldItems members are of type IList, they are not immutable and can be grown. I made the following helper to test this idea:
private NotifyCollectionChangedEventArgs CreateEventArgsWithMultiple(NotifyCollectionChangedAction action, IEnumerable items, int newStartingIndex)
{
NotifyCollectionChangedEventArgs eventArgs = null;
foreach (var item in items)
{
if (eventArgs == null)
{
eventArgs = new NotifyCollectionChangedEventArgs(action, item, newStartingIndex);
}
else
{
eventArgs.NewItems.Add(item);
}
}
return eventArgs;
}
I haven't seen any problems yet, but I'm looking for experience and input with this particular corner of Silverlight. Should I bother to batch Adds like this, or just use a Reset?
This is on Windows Phone 7.1 (Mango), by the way.
Edit: To follow up on the comment by Erno. Microsoft says in this (poorly worded) Silverlight documentation page on MSDN that it can be "generally" assumed that NewItems only has one element, and even suggests the shortcut of using NewItems[0] to access it. So they retain the IList signature for "compatibility", but then go on butcher the meaning of the type. Disappointing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有遇到任何问题,但答案是“不要这样做!” (除非您仅将参数传递给您编写的代码)。
原因(如评论中所述)是 Silverlight 中可能有代码假设只有一项。即使没有今天,也可能有明天,并且您绝对不希望您的应用程序在某些更加依赖于这一假设的新版本 Silverlight 出现时出现故障。
I haven't run into any issues, but the answer is "Don't do it!" (unless you're only passing the args to code that you've written).
The reason (as has been said in the comments) is that there may be code in Silverlight which assumes there's only one item. Even if there isn't today, there may be tomorrow, and you definitely don't want your app to break when some new version of Silverlight comes out that relies more heavily on this assumption.