在纵向模式下检测OrientationChanged事件

时间:2015-06-22 18:04:22

标签: c# xaml event-handling windows-phone windows-phone-8.1

在Windows Phone 8.1上,我正在进行OrientationChanged事件,当横向模式完美运行时,媒体元素将更改为完整窗口。但是当我旋转到肖像时,它在纵向模式下不会从整个窗口退回到屏幕的宽度。事实上,它没有做任何事情。问题是事件处理程序检测到横向方向而不是纵向方向?我错过了什么? MediaElement似乎卡在IsFullWindow = true中,并且从不再次检查orientationchanged事件方法。

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <StackPanel Width="Auto" Height="250" Background="Green" Orientation="Horizontal" VerticalAlignment="Top">
        <MediaElement x:Name="media"
                  Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
                  AutoPlay="True"
                  AreTransportControlsEnabled="True"
                  HorizontalAlignment="Center" 
                  VerticalAlignment="Top" 
                  Stretch="UniformToFill"
                  Width="Auto"
                  Height="Auto"
                      />
    </StackPanel>
</Grid>

C#

void MainPage_OrientationChanged(DisplayInformation sender, object args)
{
    var orientation = DisplayInformation.GetForCurrentView().CurrentOrientation; // get the current orientation of the display
    if (orientation == DisplayOrientations.Landscape || orientation == DisplayOrientations.LandscapeFlipped) // if the orientation is landscape...
    {
        media.IsFullWindow = true; // puts the media element in full screen while in landscape
    }
    else //if (orientation == DisplayOrientations.Portrait || orientation == DisplayOrientations.PortraitFlipped)
    {
        media.IsFullWindow = false; // puts the media element out of full screen in portrait
        //media.Width = Window.Current.Bounds.Width; // set bounds of video width to width of screen
    }
}

1 个答案:

答案 0 :(得分:1)

在您的XAML中,您可以编写以下XAML行:

<Page
    x:Class="App1.MainPage"
    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"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <MediaElement x:Name="media"
                    Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
                    AutoPlay="True"
                    AreTransportControlsEnabled="True"
                    HorizontalAlignment="Stretch" 
                    VerticalAlignment="Top" 
                    Stretch="Uniform" />
        </StackPanel>
    </Grid>
</Page>

在您的代码隐藏中,您可以调用 SimpleOrientationSensor 类的OrientationChanged事件:

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            SimpleOrientationSensor.GetDefault().OrientationChanged += (s, a) =>
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    if (a.Orientation == SimpleOrientation.NotRotated || a.Orientation == SimpleOrientation.Faceup || a.Orientation == SimpleOrientation.Facedown)
                    {
                        media.IsFullWindow = false;
                    }
                    else
                    {
                        media.IsFullWindow = true;
                    }
                });
        }
    }
}

不要忘记包含 Windows.Devices.Sensors 引用;)