如何检查启动Windows Phone 8应用程序的方向?

时间:2014-01-06 20:09:49

标签: windows-phone-8 orientation landscape-portrait

我想知道如何在启动时直接检查当前方向(MainPage LoadedEvent)。

以下代码不起作用:

    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",应用程序将从头开始直接显示在横向视图中。)

Ps。:当然,设备和模拟器在启动时处于风景中;)

2 个答案:

答案 0 :(得分:1)

您是否添加了Orientation属性?

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

答案 1 :(得分:-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"
        >