Link to home
Start Free TrialLog in
Avatar of mastiSoft
mastiSoft

asked on

Switch between Landscape or Portrait mode

hi.
How do I know if the tablet is in Landscape or Portrait mode?
I have an application written in WPF (C#) , the layout looks nice if the tablet is Landscape mode, but for Portrait mode I have to use another layout.
How do I get know app. when layout have to be switched from one to another?
Avatar of David Favor
David Favor
Flag of United States of America image

You can't.

All you can know, at any given point, are screen dimensions.

And even screen dimensions can change instantly, as someone rotates their device.

The easy way to deal with this is to use @media directives with CSS breakpoints for different devices.

So you'll never write code for landscape or portrait, you'll write code for a specific CSS breakpoint, which can change at any time.
Avatar of mastiSoft
mastiSoft

ASKER

Hi, thank you for the answer David.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are events which are fired when the display settings change; e.g. -
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new           
    EventHandler(OnDisplaySettingsChanged);

public bool IsLandscape { get; set; }

void OnDisplaySettingsChanged(object sender, EventArgs e)
{
  IsLandscape = SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight;

  RaisePropertyChanged( "IsLandscape" );
}

Open in new window

Main Window.xaml -
<Border >
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLandscape}" Value="True">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <RotateTransform Angle="90"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
         </Style>
    </Border.Style>

///The rest of your controls and UI

</Border>

Open in new window

-saige-
thank you to all for suggestions. I will test all of them and give my answer tomorrow.
You can't.

All you can know, at any given point, are screen dimensions.
Ummm... Doesn't landscape mode simply mean that the width is greater than the height (and vice versa)?

Or in other words, line 8 of it_saige's first snippet?