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

Windows phone 8 如何在启动Windows Phone 8应用程序时检查方向?,windows-phone-8,orientation,landscape-portrait,Windows Phone 8,Orientation,Landscape Portrait,我想知道如何在启动时直接检查当前方向(MainPage LoadedEvent) 以下代码不起作用: public MainPage() { InitializeComponent(); Loaded += MainPage_Loaded; } void MainPage_Loaded(object sender, RoutedEventArgs e) { Debug.WriteLine(Orient

我想知道如何在启动时直接检查当前方向(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=“GraphitorLandscape”
,应用程序从一开始就直接显示在横向视图中。)


注:当然,设备和模拟器在启动时处于横向位置;)

是否添加了方向属性

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

希望这就是你正在寻找的

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"
>

include OrientationChanged=“PhoneApplicationPage\u OrientationChanged”



这似乎就是交易。我添加了(如上所述)
SupportedOrientations=“肖像或风景”
——与您的示例相反,我添加了
Orientation=“肖像”
。如果我把它改成横向,我会遇到相反的问题,总是显示横向方向。-不知何故,我可以理解这种行为,因为它是默认位置,但我仍然想知道如何解决我的问题?也许用另一种方法?现在我认为注册OrientionChanged事件就足够了。但是还有别的办法吗?订阅OrientionChanged活动对你有帮助吗?很抱歉回复太晚了。这里发生了很多事情
OrientationChanged=“MainPage\u OnOrientationChanged”
事件订阅(基本上)有效。但如果我想确保它只在页面导航中出现一次,我必须添加讨厌的全局bool。我真的很想在构造函数中完成这项工作。但我想那就没有机会了/你能更具体地说明这个问题吗:你想在每次更改页面(导航到其他页面)时检查页面方向,还是只想在启动时检查一次方向?我认为这无关紧要。但在我的例子中,我希望在用户每次导航到页面时都进行检查(它是主页,所以在启动时也是如此)。我尝试的结果是,方向始终与您在XAML中指定的构造器、加载事件和OnNavigatedTo中的标准方向相匹配。然而,如果设备处于相反的位置,它会触发OnOrientationChanged。因此,逻辑是可以做到的。我只是希望不需要全局变量来指示它是否是第一个方向更改。
     <phone:PhoneApplicationPage
              x:Class="Orientation.MainPage"
                SupportedOrientations="PortraitOrLandscape"                  
                OrientationChanged="PhoneApplicationPage_OrientationChanged"
        >