2013-07-16 35 views
1

如果用户将其滚动到视图中,我需要自动播放媒体文件。WinRT - 确定某个元素是否对用户可见

我得到了这样的事情:

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

在那些的DataTemplates之一,我现在用的是PlayerFramework(PlayerFramework on codeplex)的媒体播放器。

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

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

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

希望你能帮助我。 在此先感谢!

朱利安

回答

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); 
    } 

这只适用于垂直滚动。如果你需要水平滚动,你需要修改方法结尾的返回值。

问候函 朱利安