C# 如何检测控件在屏幕上是否可见?

C# 如何检测控件在屏幕上是否可见?,c#,xaml,windows-phone-8,windows-phone,panorama-control,C#,Xaml,Windows Phone 8,Windows Phone,Panorama Control,我有一个Panorama控件,它有三项: <phone:Panorama> <phone:PanoramaItem> </phone:PanoramaItem> <phone:PanoramaItem> </phone:PanoramaItem> <phone:PanoramaItem Header="third item" Orientation="Horizontal">

我有一个
Panorama
控件,它有三项:

<phone:Panorama>

    <phone:PanoramaItem>

    </phone:PanoramaItem>

    <phone:PanoramaItem>

    </phone:PanoramaItem>

    <phone:PanoramaItem Header="third item" Orientation="Horizontal">
        <Grid>
            <StackPanel Margin="0,4,16,0" Orientation="Vertical" VerticalAlignment="Top">
                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                </StackPanel>
                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Margin="0,12,0,0">
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    <Border Background="Black" Height="173" Width="173" Margin="12,0,0,0"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </phone:PanoramaItem>
</phone:Panorama>


我如何知道屏幕上是否有黑色背景的最后一个
边框
?因为它是一个水平的
panorama项目
,所以我不能依赖panorama的
SelectedIndex
。有什么提示吗?

如果您只想检查某个元素是否在屏幕范围内,您可以使用VisualTreeHelper,如下所示:

Rect screenBounds = new Rect(0, 0, Application.Current.Host.Content.ActualWidth, Application.Current.Host.Content.ActualHeight);

if (VisualTreeHelper.FindElementsInHostCoordinates(screenBounds, myPanorama).Contains(elementToCheck))
    Debug.WriteLine("Element is now visible");
else
    Debug.WriteLine("Element is no longer visible");
我对XAML做了两个更改(只是为了简化示例),首先我将
全景图命名为

<phone:Panorama x:Name="myPanorama">
如果您还想在全景滚动正在更改时在事件中检查它,它似乎变得有点困难,因为如果某个控件位于其他控件之下,则
操纵delta
事件不起作用。一种解决方案是在每次报告fram触摸时进行检查。这是通过以下方式完成的:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Touch.FrameReported += Touch_FrameReported;
}

private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
    // This is the same code as above
    Rect screenBounds = new Rect(0, 0, Application.Current.Host.Content.ActualWidth, Application.Current.Host.Content.ActualHeight);

    if (VisualTreeHelper.FindElementsInHostCoordinates(screenBounds, myPanorama).Contains(elementToCheck))
        Debug.WriteLine("Element is now visible");
    else
        Debug.WriteLine("Element is no longer visible");
}
对于
visualtreeheloper
Touch
,您还需要这两个使用指令:

using System.Windows.Media;
using System.Windows.Input;
不幸的是,我现在想不出一个更简单的解决办法

编辑: 对于事件来说,这还远远不够完美,例如,如果全景是以编程方式“平移”的,或者当它捕捉时,它将不会给出预期的结果。问题是全景图是建立在
PanningLayer
PanningTitleLayer
PanningBackgroundLayer
,而不是
ScrollViewer
,因此没有
ScrollBar
,这意味着没有
Scroll
事件:(


一个解决方案是使用计时器定期检查它是否可见,但这也感觉像是一个丑陋的解决方案。

重复?-这是WP7,但我猜仍然适用否。他说的是
SelectionChanged
。当它是水平项时,该事件不会发生,所以当
Orientation=“horizontal”
对于
全景项目
?好的,我明白了。是的。我想知道黑色的
边框
何时可见。你是说只想知道它的
可见性
属性的当前状态吗?
using System.Windows.Media;
using System.Windows.Input;