【问题标题】:visual elements of current pivot page当前数据透视页面的视觉元素
【发布时间】:2012-09-11 15:52:45
【问题描述】:

我正在使用 C#、Silverlight、Visual Studio for Windows Phone 7。

我有兴趣只获取在枢轴期间当前显示在屏幕上的视觉元素。例如,我的枢轴可能有 5 个 PivotItem,但我只想获取每次轻弹后显示的项目。

现在,我可以从可视化树中获取整个枢轴,显然我可以选择一个特定的 PivotItem。但我希望能够只获得屏幕上的当前枢轴。最终目标是获取屏幕上元素的绝对位置,而不考虑当前的 PivotItem。

这可能吗?如果是这样,怎么做?

【问题讨论】:

    标签: c# silverlight windows-phone-7 pivot visual-tree


    【解决方案1】:

    Pivot 上的SelectedItem 和/或SelectedIndex 将返回当前可见的 PivotItem。

    示例代码供您说明我的观点:

    XAML:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!--Pivot Control-->
        <controls:Pivot x:Name="SomePivot" Title="MY SAMPLE PIVOT">
                <StackPanel>
                    <Button Content="Button 0"/>
                    <Button Content="Button 1"/>
                    <Button Content="Button 2"/>
                    <Button Content="Button 3"/>
                    <Button Content="Button 4"/>
                    <Button Content="Button 5"/>
                    <Button Content="Button 6"/>
                    <Button Content="Button 7"/>
                    <Button Content="Button 8"/>
                    <Button Content="Button 9"/>
                    <Button Content="Button 0"/>
                    <Button Content="Button 1"/>
                    <Button Content="Button 2"/>
                    <Button Content="Button 3"/>
                    <Button Content="Button 4"/>
                    <Button Content="Button 5"/>
                    <Button Content="Button 6"/>
                    <Button Content="Button 7"/>
                    <Button Content="Button 8"/>
                    <Button Content="Button 9"/>
                </StackPanel>
            </controls:PivotItem>
            <controls:PivotItem Header="item2">
                <Button Content="Button D" Name="B"/>
            </controls:PivotItem>
            <controls:PivotItem Header="item3">
                <Button Content="Button D" Name="C"/>
            </controls:PivotItem>
            <controls:PivotItem Header="item4">
                <Button Content="Button D" Name="D"/>
            </controls:PivotItem>
        </controls:Pivot>
    
        <Button Grid.Row="1" Content="Get Current Pivot Item" Click="GetCurrentPivotItem"/>
    </Grid>
    

    代码隐藏:

    private void GetCurrentPivotItem(object sender, RoutedEventArgs e)
    {
        PivotItem pivotItem = (PivotItem)SomePivot.SelectedItem;
        Debug.WriteLine("Pivot Item : {0}", pivotItem.Header);
        foreach (var element in VisualTreeHelper.FindElementsInHostCoordinates(new Rect(20, 0, 480, 700), pivotItem))
        {
            if (element is Button)
            {
                Debug.WriteLine("{0}", ((Button)element).Content);
            }
        }
    }
    

    这有帮助吗?

    【讨论】:

    • 谢谢!!是的,它确实!!有趣的是,我目前正在通过 LayoutUpdated 事件触发我的方法,奇怪的是它只被调用了几次。假设我有 5 个枢轴。 LayoutUpdated 在前 3 次轻弹中被触发,但之后再也不会触发。知道这是为什么吗?
    • 我已经更改了我的代码,以便由 PivotSelectionChanged 调用此方法。仍然很好奇为什么 LayoutUpdated 在停止之前只被调用了几次。
    • 为了将来参考,最好使用 LoadedPivotItem 事件而不是 PivotSelectionChanged,因为这样您就可以在元素完成旋转后获取它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多