WinRT - 确定元素是否对用户可见

时间:2013-07-16 14:16:35

标签: c# wpf xaml windows-8 windows-runtime

如果用户将媒体文件滚动到视图中,我需要自动播放媒体文件。

我有这样的事情:

<ScrollViewer>
   <ItemsControl ItemsSource="{Binding SelectedProduct.Entities}" ItemTemplateSelector="{StaticResource EntityDataTemplateSelector}" />             
</ScrollViewer>

在其中一个DataTemplates中,我正在使用PlayerFramework(PlayerFramework on codeplex)的媒体播放器。

当用户将媒体播放器(手动)滚动到视图中时。视频将开始播放。

我的问题是:如何确定元素是否在视口中?

我早早地去了this post,但它没有在winrt上工作。

希望你能帮助我。 提前谢谢!

儒略

1 个答案:

答案 0 :(得分:3)

我可以通过将方法从this post调整为:

来解决问题
private bool IsVisibileToUser ( FrameworkElement element, FrameworkElement container )
    {
        if ( element == null || container == null )
            return false;

        if ( element.Visibility != Visibility.Visible )
            return false;

        Rect elementBounds = element.TransformToVisual( container ).TransformBounds( new Rect( 0.0, 0.0, element.ActualWidth, element.ActualHeight ) );
        Rect containerBounds = new Rect( 0.0, 0.0, container.ActualWidth, container.ActualHeight );

        return (elementBounds.Top < containerBounds.Bottom && elementBounds.Bottom > containerBounds.Top);
    }

这仅适用于垂直滚动。如果需要它进行水平滚动,则需要修改方法末尾的返回值。

祝你好运 儒略

相关问题