2013-07-16 36 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); 
    } 

這隻適用於垂直滾動。如果你需要水平滾動,你需要修改方法結尾的返回值。

問候函 朱利安