【问题标题】:How to know if an item is visible to user?如何知道一个项目是否对用户可见?
【发布时间】:2013-11-18 00:31:57
【问题描述】:

请看这个:

<Grid>
    <ScrollViewer>
        <Grid Background="Red" Width="50" Height="50" VerticalAlignment="Top" Margin="0,-50,0,0"/>
    </ScrollViewer>
</Grid>

这里的红色网格是不可见的,因为它有边距。但是当用户下拉时,它会在屏幕上可见。

我怎么知道它何时可见?谢谢。

(如果重要的话,它是一个 WP8 应用程序)

【问题讨论】:

  • 如果 ScrollViewer VerticalOffset >= 50 表示其可见
  • 感谢@sa_ddam213。有没有办法知道它正在滚动?
  • 你应该订阅ScrollViewer.ScrollChanged事件。
  • @MarcinJuraszek 令人惊讶的是它在 windows phone 中不受支持
  • 您是否尝试实施拉动刷新?你见过thisthis 可能会有所帮助。 Telerik 有一个good solution

标签: wpf xaml windows-phone-7 windows-phone-8 windows-phone


【解决方案1】:

此方法可能对您很方便。

private bool IsUserVisible(FrameworkElement element, FrameworkElement container)
{
    if (!element.IsVisible)
        return false;

    Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
    Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
    return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 2012-06-24
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2011-09-01
    • 2014-02-23
    相关资源
    最近更新 更多