2011-12-06 30 views
4

這是我在橫向模式下的應用程序(以及網格內部的兩個邊界,名爲'LayoutRoot')。 enter image description hereWindows Phone在橫向模式下的座標

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認爲它處於肖像模式。如何正確接收橫向模式下的座標控制?

+4

+1只是爲了漂亮例如佈局...圖片= 10^3個字。 –

+0

您的方向如何設置在設備或應用程序上? – Keeano

+0

@KeeanoMartin:我在我的頁面中定義了SupportedOrientations =「Landscape」Orientation =「Landscape」xaml – Nagg

回答

3

看起來好像FindElementsInHostCoordinates不考慮橫向模式或存在SystemTray。只有當您使用Portrait與SystemTray.IsVisible =「False」的座標時,它纔會真正起作用。

檢查這個博客帖子從艾倫Mendelevich瞭解更多詳情:

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

需要進行的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> 
+0

謝謝你的回答。奇怪的行爲。 – Nagg