Coordinates in landscape mode in Windows Phone

Here is my landscape mode app (and two borders inside the grid called LayoutRoot). enter image description here

1) I am trying to get the coordinates of border1 as follows:

  GeneralTransform generalTransform = border1.TransformToVisual(LayoutRoot); Point point = generalTransform.Transform(new Point(0, 0)); 

and it returns to me the expected coordinates of the point: X = 0, Y = 380

2) Now I'm trying to get the same border1 at these coordinates:

 var controls = VisualTreeHelper.FindElementsInHostCoordinates( point, LayoutRoot).ToArray(); 

And suddenly I got border2 ! It seems that FindElementsInHostCoordinates thinks this is in portrait mode. How can I get coordinate control in landscape mode correctly?

+4
source share
1 answer

FindElementsInHostCoordinates does not seem to take into account landscape mode or the existence of a SystemTray. This really works when you use the coordinates for Portrait with SystemTray.IsVisible = "False".

Check out this blog post from Alan Mendelevich for more details:

http://devblog.ailon.org/devblog/post/2011/04/03/Obstruction-Detection-in-Silverlight-for-Windows-Phone.aspx

You need to do something similar to this + account for SystemTray sizes, if visible.

Code example:

 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> 
+3
source

Source: https://habr.com/ru/post/1384936/


All Articles