3

我想更改方向属性,如下所示:

<Style x:Key="FlipViewStyleV" TargetType="FlipView">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel AreScrollSnapPointsRegular="True" Orientation="Vertical" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想让它在设备方向改变时工作,方向属性也应该跟随变化。但是当我在代码隐藏中添加一个按钮以将方向从垂直更改为水平时它不起作用,有人知道吗?

4

1 回答 1

2

看起来更改 a 的属性Orientation由于某种原因不起作用。所以这里有一个替代方案。FlipViewItemsPanel

您将需要复制您的FlipView. 一个将实现 a Vertical VirtualizingStackPanel,另一个将实现 a Horizontal VirtualizingStackPanel

在页面的Resources.

<Page.Resources>
    <ItemsPanelTemplate x:Key="HorizontalItemsPanel">
        <VirtualizingStackPanel AreScrollSnapPointsRegular="True" Orientation="Horizontal" />
    </ItemsPanelTemplate>
    <ItemsPanelTemplate x:Key="VerticalItemsPanel">
        <VirtualizingStackPanel AreScrollSnapPointsRegular="True" Orientation="Vertical" />
    </ItemsPanelTemplate>
</Page.Resources>

然后,您将要使用SimpleOrientationSensor来监控手机的方向变化。

private SimpleOrientationSensor _orientationSensor = SimpleOrientationSensor.GetDefault();

在你订阅了它的OrientationChanged事件之后,

 _orientationSensor.OrientationChanged += OrientationSensor_OrientationChanged;

在其回调中,只需FlipView相应地隐藏和显示 s 。

private async void OrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
{
    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            switch (args.Orientation)
            {
                case SimpleOrientation.NotRotated:
                case SimpleOrientation.Rotated180DegreesCounterclockwise:
                    this.HorizontalFlipView.Visibility = Visibility.Visible;
                    this.VerticalFlipView.Visibility = Visibility.Collapsed;
                    break;
                case SimpleOrientation.Rotated90DegreesCounterclockwise:
                case SimpleOrientation.Rotated270DegreesCounterclockwise:
                    this.HorizontalFlipView.Visibility = Visibility.Collapsed;
                    this.VerticalFlipView.Visibility = Visibility.Visible;
                    break;
            }
        });
}
于 2014-12-19T12:30:37.393 回答