john chen
john chen

Reputation: 176

Windows Phone 8 Page Orientation

How can I detect orientation when the page has been loaded or while the page is loading? I implemented OrientationChanged method. But when I set the first page as landscape, the second page doesnt trigger this method. Page is in landscape mode but UI is not. I mean page orientation is ok, but musnt OrientationChanged be triggered? I change the appearance of the UI objects in this method. If it is not triggered, UI will shown like in portrait mode.

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    if (e.Orientation == PageOrientation.Landscape || e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
    {
        SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
    }
    else
    {
        SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    }
}

How can I solve this issue?

Upvotes: 1

Views: 71

Answers (2)

azarc3
azarc3

Reputation: 1306

Did you wire up the event to the event handler? You should have something similar to the following in your constructor....

this.OrientationChanged += PhoneApplicationPage_OrientationChanged;

Upvotes: 0

Kevin Gosse
Kevin Gosse

Reputation: 39007

Simply put your code in a distinct method, and call this method both from the OrientationChanged and Loaded events:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    this.SetOrientation(this.Orientation);
}

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    this.SetOrientation(e.Orientation);
}

private void SetOrientation(PageOrientation orientation)
{
    if (orientation == PageOrientation.Landscape || orientation == PageOrientation.LandscapeLeft || orientation == PageOrientation.LandscapeRight)
    {
        SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
    }
    else
    {
        SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    }
}

Upvotes: 4

Related Questions