获取Windows Phone 8.1 / Windows 8.1的当前可见Hub部分

时间:2014-05-16 13:27:57

标签: c# windows windows-phone-8 windows-runtime windows-phone

我是否可以在Windows 8.1 / Windows Phone 8.1中的Hub控件上绑定一个属性,以便我可以获取当前选择的HubSection?通常对于TabControl,可以设置SelectedIndex属性。但是,Hub控件似乎不存在这种情况。

到目前为止,我能找到的唯一属性是SectionsInView属性。但它是只读的,不能通过DataBinding绑定。

enter image description here

1 个答案:

答案 0 :(得分:1)

找到一种方法来解决这个问题,但遗憾的是它没有本地属性。

所以我做的是:

        private int CurrentSectionIndex = 0;
        private HubSection CurrentSection
        {
            get { return this.Hub.SectionsInView[CurrentSectionIndex]; }
        }

        //Retrieve the ScrollViewer of the Hub control and handle the ViewChanged event of this ScrollViewer
        public Page()
        {
            ScrollViewer sv = ControlHelper.GetChild<ScrollViewer>(this.Hub);
            sv.ViewChanged += sv_ViewChanged;
        }
        //In the ViewChanged event handler body then calculate the index relative to the horizontal offset (means current position) of the ScrollViewer
        void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            if(!e.IsIntermediate)
            {
                double ho = ControlHelper.GetChild<ScrollViewer>(this.Hub).HorizontalOffset;

                if (ho >= CurrentSection.ActualWidth - (this.ActualWidth - CurrentSection.ActualWidth))
                {
                    CurrentSectionIndex = (int)((ho + (this.ActualWidth - CurrentSection.ActualWidth)) / CurrentSection.ActualWidth);
                }
            }
        }

似乎工作但仍然要测试(让我知道)