How to get the device orientation changed event under Windows Phone

你。 提交于 2019-12-19 10:46:23

问题


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:


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)
{
}


来源:https://stackoverflow.com/questions/30767273/how-to-get-the-device-orientation-changed-event-under-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!