WPF Orientation

I am developing an application on a tablet with portrait orientation.

However, when the tablet goes into landscape mode, the application also rotates and all alignments are discarded. So is it possible to somehow block my WPF application to one orientation?

Thanks!

+6
source share
4 answers

I have to agree with Martin : I myself have developed applications for tablet PCs, and you better provide a layout that works well in landscape and portrait.

In addition, you can detect a change in orientation this way:

Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); } void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight) { // runs in landscape } else { // runs in portrait } } 
+6
source

Sven does an excellent job of detecting a change in orientation ...

However, if you are not writing a Metro application (where you can set preferred orientations in the manifest), there is no real way to NOT allow orientation changes, however, if you are only interested in letting the portrait go, you could do something like this:

View Model:

 Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); } public bool IsLandscape { get; set; } void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight) { IsLandscape = true; } else { IsLandscape = false; } RaisePropertyChanged( "IsLandscape" ); } 

In the main .xaml window:

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

Thus, we really do not limit Orientation, we just notice when this happens, and rotate our user interface so that it still looks like in portrait mode :) Again, this is mainly for applications that do not support Metro Win 8, and applications that also run on Win 7 tablets.

+2
source

I ran into the same problem when developing a wpf application for tablets and found this article from msdn explaining how to determine screen rotation and orientation: http://msdn.microsoft.com/en-us/library/ms812142.aspx

0
source

I do not know of any open API for locking screen orientation. Mostly because, as a rule, tablet makers provided their own pre-installed utilities or drivers that used accelerometer data to change orientation. This was not a built-in OS feature. This may change in Windows 8.

As long as it does not match the orientation of the lock, you can try to respond to changes in orientation by adding a rotary transformation to your LayoutTransform root container. This would change the layout space, so your application thought it was still rotating 90 degrees, but the rest of the OS would not agree. Therefore, it is really practical only for full-screen applications.

0
source

Source: https://habr.com/ru/post/902736/


All Articles