2014-01-06 211 views
0

我不知道如何直接在啓動時檢查當前方向(MainPage LoadedEvent)。如何檢查啓動Windows Phone 8應用程序的方向?

下面的代碼無法正常工作:

public MainPage() 
    { 
     InitializeComponent(); 

     Loaded += MainPage_Loaded; 

    } 

    void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     Debug.WriteLine(Orientation); 
     if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft) 
      Debug.WriteLine("Simulator and Device won't jump in here on startup (always PortraitUp)..."); 
    } 

它爲什麼這樣的行爲?我在哪裏可以正確定位方向?

(我已啓用SupportedOrientations="PortraitOrLandscape"和應用程序在橫向視圖從一開始就直接顯示。)

詩:當然器件和模擬器是在啓動景觀;)

回答

1

你加方向屬性?

<phone:PhoneApplicationPage 
    x:Class="Orientation.MainPage" 
    SupportedOrientations="PortraitOrLandscape" Orientation="Landscape" 
    > 
+0

這似乎是交易。我加了(如上所述)'SupportedOrientations =「PortraitOrLandscape」' - 與你的例子相比,我有'Orientation =「Portrait」'。如果我將其改爲「風景」,我會遇到相反的問題,始終顯示風景方向。 - 不知何故,我可以理解這種行爲,因爲它是默認位置,但我仍然想知道我該如何解決我的問題?也許用不同的方法? 現在我認爲註冊orientationChanged事件可能就足夠了。但還有另一種方式嗎? – malte

+0

訂閱OrientationChanged事件對您有幫助嗎? –

+0

對不起,延遲迴復。很多東西都在這裏atm。 'OrientationChanged =「MainPage_OnOrientationChanged」'事件訂閱(基本上)有效。但如果我想確保它只在頁面導航中出現一次,我必須添加令人討厭的全局布爾值。 我真的很想在構造函數中完成此操作。但我認爲那沒有機會了? :/ – malte

-1

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB0QFjAA&url=https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fwindows%2Fapps%2Fjj207002%2528v%3Dvs.105%2529.aspx&ei=i95SVeCDG8XJuASUwYGYDQ&usg=AFQjCNGC1xSQUv7Ge6t0hbGP0hfdV8dYtw

希望這是你正在尋找

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) 
{ 

if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait)) 
{ 
    // if portrait 
} 

else 
{ 
    // If landscape 
} 
} 
在下面的XAML代碼

<phone:PhoneApplicationPage 
x:Class="Orientation.MainPage" 
SupportedOrientations="PortraitOrLandscape" Orientation="Landscape" 
> 

包括OrientationChanged = 「PhoneApplicationPage_OrientationChanged」

 <phone:PhoneApplicationPage 
       x:Class="Orientation.MainPage" 
       SupportedOrientations="PortraitOrLandscape"     
       OrientationChanged="PhoneApplicationPage_OrientationChanged" 
     > 
+0

儘管此鏈接可能會回答問題,但最好在此處包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Anthon

+0

編輯了我的答案。 –

相關問題