C# Windows Phone中横向模式下的坐标
这是我在横向模式下的应用程序(网格中的两个边界称为“LayoutRoot”)。 1) 我试图通过以下方式接收C# Windows Phone中横向模式下的坐标,c#,silverlight,windows-phone-7,coordinates,landscape,C#,Silverlight,Windows Phone 7,Coordinates,Landscape,这是我在横向模式下的应用程序(网格中的两个边界称为“LayoutRoot”)。 1) 我试图通过以下方式接收border1的坐标: GeneralTransform generalTransform = border1.TransformToVisual(LayoutRoot); Point point = generalTransform.Transform(new Point(0, 0)); 它返回我期望的点坐标:X=0,Y=380 2) 现在我试图通过这些坐标接收相同的
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
认为它处于纵向模式。如何在横向模式下正确接收坐标控制?似乎FindElementsInHostCoordinates不考虑横向模式或系统托盘的存在。只有当您使用坐标与SystemTray进行纵向比较时,它才真正起作用。IsVisible=“False”
查看Alan Mendelevich的这篇博文,了解更多详细信息:
如果系统托盘尺寸可见,则需要执行类似的操作+说明系统托盘尺寸
示例代码:
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:
+1仅针对漂亮的示例布局。。。Picture=10^3个单词。您的方向是如何在设备或应用程序上设置的?@KeenanoMartin:我支持我的页面中定义的Orientations=“横向”Orientations=“横向”您是否考虑过简单的转换?(portX=maxPortX-landY;portY=landX)左侧和(portX=landY;portY=maxPortY-landX)右侧。谢谢您的回答。奇怪的行为。
<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>