这是我在横向模式下的应用程序(以及网格内的两个边框,名为'LayoutRoot')。
1)我试图以这种方式接收border1
的坐标:
GeneralTransform generalTransform = border1.TransformToVisual(LayoutRoot);
Point point = generalTransform.Transform(new Point(0, 0));
它返回我预期的点坐标: X = 0,Y = 380
2)现在我试图通过这些坐标接收相同的border1
:
var controls = VisualTreeHelper.FindElementsInHostCoordinates(
point, LayoutRoot).ToArray();
我突然收到了border2
!似乎FindElementsInHostCoordinates
认为它处于纵向模式。如何正确地以横向模式通过坐标接收控件?
答案 0 :(得分:3)
似乎FindElementsInHostCoordinates不考虑横向模式或SystemTray的存在。它只适用于使用带有SystemTray.IsVisible =“False”的Portrait坐标时。
查看Alan Mendelevich的博文,了解更多详情:
如果可见,则需要为SystemTray维度执行与此+帐户类似的操作。
示例代码:
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace PhoneApp4
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
GeneralTransform generalTransform = border1.TransformToVisual(LayoutRoot);
Point point = generalTransform.Transform(new Point(1, 1));
var controls = FindElementsAtCoordinates(point);
}
private UIElement[] FindElementsAtCoordinates(Point point)
{
if ((this.Orientation & PageOrientation.Portrait) == 0)
{
if (this.Orientation == PageOrientation.LandscapeLeft)
point = new Point(
this.ActualHeight - point.Y,
point.X + (SystemTray.IsVisible ? 72 : 0));
else
point = new Point(
point.Y,
this.ActualWidth - point.X + (SystemTray.IsVisible ? 72 : 0));
}
return VisualTreeHelper.FindElementsInHostCoordinates(
new Point(point.X, point.Y + (SystemTray.IsVisible ? 72 : 0)),
page).ToArray();
}
}
}
XAML:
<phone:PhoneApplicationPage
x:Class="PhoneApp4.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Landscape"
Orientation="Landscape"
mc:Ignorable="d"
d:DesignHeight="480"
d:DesignWidth="728"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid
x:Name="LayoutRoot"
Background="Transparent">
<Border
x:Name="border1"
Width="100"
Height="100"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
BorderThickness="5"
BorderBrush="Red" />
<Border
x:Name="border2"
Width="100"
Height="100"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
BorderThickness="5"
BorderBrush="Orange" />
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Minimized">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>