当 SelectedIndex >= 2 (wp7) 时,更新绑定到 Pivot 的 ObservableCollection 会崩溃
我有一个 observableCollection 绑定到 UI 中的枢轴控件。当我尝试更新集合(clear() 项目并重新创建)时,除非枢轴控件的 selectedIndex 大于或等于 2,否则一切正常。
在这种情况下,当我尝试调用 Add() 到集合时,我会收到 ArgumentOutOfRange 异常。可观察的集合。这很奇怪。
我尝试创建一个新的可观察集合,然后在那里添加()项目,这似乎有效,但除非我调用更新函数两次,否则我不会刷新用户界面。
有什么问题吗?这是一个错误吗?
I have an observableCollection bound to a pivot control in my UI. When I try to update the collection (clear() items and recreate) everything works fine unless the selectedIndex of the pivot control is bigger or equal to 2.
In that case I get an ArgumentOutOfRange exception when I try to call Add() to the observable collection. It is very strange.
I tried creating a new observable collection and then Add() items there, this seems to work but I doesn't refresh the UI unless I call my update function twice.
What can be wrong? Is this a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个已知问题。
将透视控件 SelectedItem/SelectedIndex 属性设置为第三个透视项 (WP7) 时出现未处理的异常
不将导航/(绑定?)推迟到加载的事件是一种解决方法。
This is a known issue.
Unhandled Exception When Setting Pivot Control SelectedItem/SelectedIndex Property to 3rd Pivot Item (WP7)
Not deferring navigation/(binding?) to the loaded event is a workaround.
您可能已经解决了这个问题,但这就是我所做的。
如前所述,这是一个已知的“错误”/限制。
不过,您可以在页面的 Loaded 事件中设置 SelectedIndex。
参见这里:
http://christian-helle.blogspot.com/2011 /02/working-around-pivot-selectedindex.html
这对我很有帮助,现在工作得很好 =)
You have probably alreade solved it but here is what I did.
As mentioned before this is a known "bug"/limitation.
You can however set the SelectedIndex in the Loaded event for the page.
See here:
http://christian-helle.blogspot.com/2011/02/working-around-pivot-selectedindex.html
This helped me and it works just fine now =)
为了尝试节省负载和性能开销,框架仅加载当前显示的枢轴和两侧的枢轴。当显示相邻项目时,其他项目会延迟加载。因此,当尝试将 SelectedItem 设置为尚未加载的项目或页面尚未完全加载时,您可能会遇到问题。
如果您可以分享一些代码来演示您想要做什么,我们也许能够提供一些更具体的帮助。
To try and save load and performacne overhead, the framework only loads the currently displayed pivot and the ones either side. The other items are delay loaded when the neighbouring item is displayed. As a consequence of this you can experience issues when trying to set the SelectedItem to an item which hasn't been loaded or the page hasn't finished loading fully.
If you can share some code to demonstrate what you're trying to do we may be able to provide some more specific help.
如前所述。 Pivot 控件经过优化,不会加载所有面板。如果您正在尝试我认为您正在尝试的内容,那么我建议您切换到全景控件,该控件会初始化所有全景项目。
As mentioned. The Pivot control is optimised to not load all the panels. If you are trying what I think you're trying, then I would suggest you switch to a Panorama Control which initalises all the PanoramaItems.
我将尝试@Jimmy Engtröm 建议的修复。不过,我也可以通过等待加载发生来解决这个问题。
在页面的代码后面:
现在,为什么是故事板?好吧,当你等到加载时,你会看到第一个枢轴,这是蹩脚的。因此故事板会快速淡入……足以掩盖修复。我尝试只设置可见性,但这会使应用程序崩溃。另请注意,出于设计目的,我在枢轴控件的 XAML 中将不透明度设置为 1。这是情节提要:
这是辅助函数(放置在单独的类文件中并引用,例如使用 MyApp.Helpers 并且类文件需要引用 System.Collections.Generic)
同样,这不是最好的修复,但它可以正常工作并且淡入实际上是我可以在其他地方使用的东西。
I'm going to try the fix suggested @Jimmy Engtröm. However, I've also been able to work around this by waiting until the Load has occurred.
And in the page's code behind:
Now, why the Storyboard? Well, when you wait until Load, you will see the first pivot and that's lame. So the Storyboard does a quick fade-in...just enough to disguise the fix. I tried just setting Visibility, but that would crash the app. Also note that, for design purposes, I leave Opacity set to 1 in the pivot control's XAML. Here's the Storyboard:
Here are the helper functions (placed in a separate class file and referenced, e.g. using MyApp.Helpers and the class file needs to reference System.Collections.Generic)
Again, it's not the greatest fix but it works alright and the fade-in is actually something I might employ elsewhere.