windows-phone-7 – 如何检查列表的数据绑定何时完成? (WP7)

前端之家收集整理的这篇文章主要介绍了windows-phone-7 – 如何检查列表的数据绑定何时完成? (WP7)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个数据透视控件,其项目包含一个包含项目的列表框.
当我滚动到下一个透视项目时,数据绑定需要一些时间,我想知道数据绑定何时完成,因为我需要启用菜单栏,一旦列表框准备好出现.
我找不到可以帮助我的活动.我尝试了列表框的Loaded事件,但它适用于某些枢轴项目,对于其他一些它不会触发!
我也尝试过布局更新事件,但它被解雇了很多次,它无法帮助我.

我能做什么?
谢谢

解决方法

要在快速滚动透视项目时确保良好的性能,您应该等待绑定透视项目的内容,直到SelectedIndex更改为止.这样,当用户在Pivot项之间快速滑动时,它不会尝试绑定;它只会在您停止在Pivot项目上时绑定.

然后,您应该在LayoutUpdated事件中的Pivot项中设置ListBox的ItemsSource属性.我使用以下扩展方法

public static void InvokeOnLayoutUpdated(this FrameworkElement element,Action action)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            else if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            // Create an event handler that unhooks itself before calling the
            // action and then attach it to the LayoutUpdated event.
            EventHandler handler = null;
            handler = (s,e) =>
            {
                element.LayoutUpdated -= handler;
                action();
            };
            element.LayoutUpdated += handler;
        }

那么你会得到一些看起来像这样的代码

pivot.InvokeOnLayoutUpdate(() =>
  {
    Dispatcher.BeginInvoke(() => 
      {
        list.ItemsSource = source;
        ApplicationBar.IsMenuEnabled = true;
      });
  });

猜你在找的Windows相关文章