2012-02-02 59 views
1

我目前正在編寫一個只有肖像的應用程序,但我有一個客戶的要求,他們想要實現一個特殊功能,如果電話打開一邊。有沒有辦法找出我的方向是什麼?

要清楚他們不希望頁面改變方向 - 所以保持頁面作爲肖像在這裏運行良好 - 但他們確實希望能夠檢測橫向變化。

有沒有辦法找到它(例如從根框架或其他對象?)還是必須訪問加速度計數據並自己完成?


要明確這一點,...

  • 我試圖保持頁面的肖像在任何時候。
  • 如果我指定SupportedOrientations =「portraitorlandscape」,那麼保持縱向頁面似乎很難(糾正我,如果我錯了,但它似乎不想留在肖像 - MS SDK也是善於製作頁轉到橫向)
  • ,如果我不指定SupportedOrientations =「portraitorlandscape」那我就不在任何頁面或RootFrame得到調用OnOrientationChanged

而作爲蛋糕上的糖霜...我需要手機也保持肖像模式 - 我需要SystemTray留在屏幕的頂部(肖像頂部)。

回答

2

您可以處理OnOrientationChanged事件,這將返回一個PageOrientation枚舉。


接受這一點,因爲評論:

@Stuart - 你會發現在這個入門套件取向Helper類是有用的。它使用加速度計,所以我想你必須使用它,但它可以節省你的時間推出自己的版本:http://msdn.microsoft.com/en-us/library/gg442298%28VS.92%29.aspx#Customizing_Behavior

+0

謝謝 - 但你只會得到,如果你支持更多比肖像方向。 (我也試過這樣做,然後不把消息傳遞給基類......它變得凌亂了!) – Stuart 2012-02-02 13:04:57

+0

啊,有趣的是,即使'SupportedOrientation'沒有設置,我認爲它會觸發。 '在旋轉手機或SupportedOrientations屬性發生變化後發生。' – keyboardP 2012-02-02 13:11:06

+1

@Stuart - 您可能會發現此入門工具包中的'Orientation Helper'類有用。它使用加速度計,所以我想你必須使用它,但它可以節省你的時間推出自己的版本:http://msdn.microsoft.com/en-us/library/gg442298%28VS.92% 29.aspx#Customizing_Behavior – keyboardP 2012-02-02 13:19:16

0

我必須在我的一個應用程序中做類似的事情之前,被用作背景不旋轉但頁面上的其他項目。
代碼看起來有點像這樣:

protected override void OnOrientationChanged(OrientationChangedEventArgs e) 
    { 
     // Keep the image in the same position as in portrait 
     // But still allows other controls to rotate when orientation changes. 
     switch (e.Orientation) 
     { 
      case PageOrientation.LandscapeRight: 
       ForegroundImage.RenderTransform = new CompositeTransform { Rotation = 90 }; 
       ForegroundImage.RenderTransformOrigin = new Point(0.5, 0.5); 
       ForegroundImage.Margin = new Thickness(158.592, -158.792, 158.592, -160.558); 

       break; 
      case PageOrientation.LandscapeLeft: 
       ForegroundImage.RenderTransform = new CompositeTransform { Rotation = 270 }; 
       ForegroundImage.RenderTransformOrigin = new Point(0.5, 0.5); 
       ForegroundImage.Margin = new Thickness(158.592, -158.792, 158.592, -160.558); 

       break; 
      default: // case PageOrientation.PortraitUp: 
       ForegroundImage.RenderTransform = null; 
       ForegroundImage.RenderTransformOrigin = new Point(0, 0); 
       ForegroundImage.Margin = new Thickness(); 

       break; 
     } 

     base.OnOrientationChanged(e); 
    } 

不幸的身邊沒有真正的工作,爲系統托盤或應用程序欄。對於系統托盤,您可以隱藏它,然後僅當用戶在屏幕的該部分附近輕敲或滑動時才顯示它(一段時間)。

1

這可能會有所幫助,但是對於到達此特定頁面的情況,而不是初始頁面 - 只能部分回答問題。它觸發OnOrientationChanged,儘管沒有改變! (在試圖找到兩天解決方案後找出此解決方案):

在特定頁面上,輸入。 xaml代碼

Orientation="None" 

在.xaml上。CS側,下

InitializeComponent(); 

Orientation = this.Orientation; 
this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>   
(OnOrientationChanged); 

並單獨

void OnOrientationChanged(object sender, OrientationChangedEventArgs e) 
{ 
if ((e.Orientation & PageOrientation.Landscape) != 0) 
    { 
      MyImage.Height = 480; //for example 
    } 

     { 
      MyImage.Width = 480; // for example 
     } 
} 

寫在我的情況,我放置在圖像如下:

<Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <StackPanel> 
     <Image x:Name="MyImage"/> 
    </StackPanel> 

..其次是其他代碼,仍在加載期間顯示實時圖片...

這會減少處於橫向模式時的圖像尺寸進入頁面!

得到了解決,最後,在看到Jeff Prosises site

相關問題