How to get the device orientation changed event under Windows Phone

3.5k Views Asked by At

With windows phone, is there an event I can register for when the device entered landscape mode?

The reason why I am asking this is because we have a view with an input box. And when in landscape mode, the TextBox is partially blocked by the keyboard. So I am thinking may have to hide some additional information on the page when it is in landscape mode (for example, hide the title of the page and etc.).

The following is a simple example. Left: Before keyboard is shown; Right: After keyboard is shown.


I posted another question related to this and has a better solution as far as I am concerned:

Why isn't the TextBox inside ContentDialog automatically scroll above keyboard

But no matter what, here is the complete code for orientation change event:

// Define this in the class 
private SimpleOrientationSensor _simpleorientation;

// Put hits in the Constructor
_simpleorientation = SimpleOrientationSensor.GetDefault();
if (_simpleorientation != null)
{
    _simpleorientation.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);
}

// Event function
private void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
          // ...
    });
}
1

There are 1 best solutions below

0
On BEST ANSWER

Your best bet would be describing to Windows.Current.SizeChanged event and testing if width is more than height. There is also a sensor for this, but is is a bit problematic, take a look at http://www.jayway.com/2014/10/06/detecting-orientation-in-universal-apps-windows-phone-8-1/.

.xaml

<ContentDialog
    x:Class="App1.ContentDialog1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    SizeChanged="SizeChangedEvent">

    <--! Other Code -->

</ContentDialog>

.cs

private void SizeChangedEvent(object sender, SizeChangedEventArgs e)
{
}