【问题标题】:Detecting OrientationChanged Event in Portrait Mode在纵向模式下检测 OrientationChanged 事件
【发布时间】:2015-06-22 18:04:22
【问题描述】:

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

【问题讨论】:

  • 您使用的是 Windows Phone 8.1 还是 Silverlight 8.1?

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


【解决方案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 参考 ;)

【讨论】:

  • 直接在 Lumia 920 设备上测试。
  • 还是不行。我还收到一条警告消息,指出Because this call is not awaited execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 我应该担心吗?如何消除此警告?
  • @TheAmazingKnight 不要担心等待警告(那是因为我将方法放在构造函数中:P)。你是在设备上测试还是在模拟器上测试?在真正的手机对我有用。 FaceUp 或 Down 在这里并不是必须的。
  • 好的。在我的 Lumia 1020 上进行了测试,仍然无法旋转回纵向模式。
  • 另一个有用的见解:我还注意到上面帖子中的代码,当媒体设置IsFullWindow = true; 时,它无法识别或响应OrientationChanged 事件。当媒体设置IsFullWindow = false;时,它响应纵向和横向旋转。当发生旋转变化时,有没有办法强制它从完整窗口中出来?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多