简体   繁体   中英

wp7 - set page orientation or alternative

I have tried but can't seem to set the page orientation of a wp7 app in code behind. I'm assuming it can't be done but I thought I would check here.

Can it be done?

If not here is my issue which someone maybe be able to help with. I created an app which I have set to Landscape and I have set PageOrientation to LandscapeLeft. Due to the phone moving it obviously rotates sometimes to LandscapeRight which I don't want it too. So I did this

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
     if(e.Orientation == PageOrientation.LandscapeLeft)
          base.OnOrientationChanged(e); 
}

Problem solved, how ever, I showed my friends the app and they had a play. A few of them preferred it to be the other way round (LandscapeRight) due to the phone buttons and the way they held the phone.

To satisfy the users of my app I wanted to put a setting which they can change based on if they wanted LandscapeLeft or LandscapeRight. As I can't change the orientation in code behind how could I accomplish this?

I did try messing around with screen rotation which I sort of got working ie app is always set to LandscapeLeft and if they wanted it LandscapeRight then simply rotate transform 180*. But the main issue was MessageBox's would appear upside down.

You can set the page orientation in code behind

 this.Orientation = PageOrientation.Landscape;

See property http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.phoneapplicationpage.orientation(v=vs.92).aspx

For example: http://www.kunal-chowdhury.com/2011/10/know-about-wp7-page-orientation-and.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+kunal2383+%28Kunal%27s+Blog%29

However, this is limited only to setting Landscape or Portrait - it seems to ignore the PortraitUp and PortraitDown and the LandscapeLeft and LandscapeRight.


It seems like the best you can do is to force the phone into Landscape and to then use a rotate transform - eg for a full screen page (no system tray or app bar) then this works to flip-flop the page between left and right landscape:

    private bool t;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SupportedOrientations = SupportedPageOrientation.Landscape;
        Orientation = PageOrientation.Landscape;

        if (t)
        {
            t = false;
            this.RenderTransform = new RotateTransform() {Angle = 180, CenterX = 400, CenterY = 240};
        }
        else
        {
            t = true;
            this.RenderTransform = null;
        }
    }

That was with Xaml:

<phone:PhoneApplicationPage 
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="480"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape"  Orientation="Landscape"
    shell:SystemTray.IsVisible="False">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

            <Button Click="Button_Click" Content="one"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
    </Grid>

</phone:PhoneApplicationPage>

Just set the SupportedOrientations property to Landscape. That way, whether your user holds it LandscapeLeft or LandscapeRight, it will just work. There is no way to programmatically set one or the other .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM