当 SelectedIndex >= 2 (wp7) 时,更新绑定到 Pivot 的 ObservableCollection 会崩溃

发布于 2024-10-08 21:23:52 字数 275 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流心雨 2024-10-15 21:23:52

这是一个已知问题。

将透视控件 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.

飘过的浮云 2024-10-15 21:23:52

您可能已经解决了这个问题,但这就是我所做的。
如前所述,这是一个已知的“错误”/限制。
不过,您可以在页面的 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 =)

掀纱窥君容 2024-10-15 21:23:52

为了尝试节省负载和性能开销,框架仅加载当前显示的枢轴和两侧的枢轴。当显示相邻项目时,其他项目会延迟加载。因此,当尝试将 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.

白色秋天 2024-10-15 21:23:52

如前所述。 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.

做个ˇ局外人 2024-10-15 21:23:52

我将尝试@Jimmy Engtröm 建议的修复。不过,我也可以通过等待加载发生来解决这个问题。

<controls:Pivot x:Name="pivotCtrl" Title="{Binding ApplicationTitle}" 
Loaded="OnPivotControlLoaded" Opacity="1">

在页面的代码后面:

private void OnPivotControlLoaded(object sender, RoutedEventArgs e)
{
    // Restore the Pivot control's SelectedIndex
    if (State.ContainsKey(SelectedPivotIndexKey))
    {
        pivotCtrl.SelectedIndex = State.Get<int>(SelectedPivotIndexKey);
    }

    myStoryboard.Begin();
}

现在,为什么是故事板?好吧,当你等到加载时,你会看到第一个枢轴,这是蹩脚的。因此故事板会快速淡入……足以掩盖修复。我尝试只设置可见性,但这会使应用程序崩溃。另请注意,出于设计目的,我在枢轴控件的 XAML 中将不透明度设置为 1。这是情节提要:

<Storyboard x:Name="myStoryboard">
    <DoubleAnimation
        Storyboard.TargetName="pivotCtrl"
        Storyboard.TargetProperty="Opacity"
        From="0.0" To="1.0" Duration="0:0:01"
        />
</Storyboard>

这是辅助函数(放置在单独的类文件中并引用,例如使用 MyApp.Helpers 并且类文件需要引用 System.Collections.Generic)

public static T Get<T>(this IDictionary<string, object> dictionary, string key)
{
    return (T)dictionary[key];
}

public static void AddOrReplace<T>(this IDictionary<string, T> dictionary, string key, T item)
{
    if (dictionary.ContainsKey(key))
        dictionary.Remove(key);

    dictionary.Add(key, item);
}

同样,这不是最好的修复,但它可以正常工作并且淡入实际上是我可以在其他地方使用的东西。

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.

<controls:Pivot x:Name="pivotCtrl" Title="{Binding ApplicationTitle}" 
Loaded="OnPivotControlLoaded" Opacity="1">

And in the page's code behind:

private void OnPivotControlLoaded(object sender, RoutedEventArgs e)
{
    // Restore the Pivot control's SelectedIndex
    if (State.ContainsKey(SelectedPivotIndexKey))
    {
        pivotCtrl.SelectedIndex = State.Get<int>(SelectedPivotIndexKey);
    }

    myStoryboard.Begin();
}

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:

<Storyboard x:Name="myStoryboard">
    <DoubleAnimation
        Storyboard.TargetName="pivotCtrl"
        Storyboard.TargetProperty="Opacity"
        From="0.0" To="1.0" Duration="0:0:01"
        />
</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)

public static T Get<T>(this IDictionary<string, object> dictionary, string key)
{
    return (T)dictionary[key];
}

public static void AddOrReplace<T>(this IDictionary<string, T> dictionary, string key, T item)
{
    if (dictionary.ContainsKey(key))
        dictionary.Remove(key);

    dictionary.Add(key, item);
}

Again, it's not the greatest fix but it works alright and the fade-in is actually something I might employ elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文