在Windows Phone UWP上旋转软屏幕按钮

时间:2016-12-07 14:21:25

标签: c# button uwp screen-orientation device-orientation

当C#,windows phone 10中的方向发生变化时,有没有人知道如何在屏幕按钮上旋转柔和?

我希望将显示方向保持为纵向或横向,以避免在显示方向更改时出现动画。我可以在DeviceOrientation.OrientationChanged事件上旋转所有内容按钮,但我不知道是否有办法访问和旋转屏幕按钮。

我找到了标记为像这样的答案的severan答案: UWP C# Disable Orientation Change Animation 但他们都谈论如何检测方向是否改变。我找不到任何关于如何在屏幕按钮上旋转软件的示例。

效果应该像Windows手机原生相机一样。

1 个答案:

答案 0 :(得分:0)

您可以使用方向传感器来获取设备的方向,然后设置按钮的投影天使

参考链接:https://msdn.microsoft.com/en-us/windows/uwp/devices-sensors/use-the-orientation-sensor

public sealed partial class MainPage : Page
{
    private SimpleOrientationSensor simpleorientation;
    private double? Current = 0;

    public MainPage()
    {
        this.InitializeComponent();
        simpleorientation = SimpleOrientationSensor.GetDefault();
        if (simpleorientation != null)
        {
            simpleorientation.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);
        }
    }

    private async void OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            SimpleOrientation orientation = args.Orientation;
            switch (orientation)
            {
                case SimpleOrientation.NotRotated:
                    Rotate(0);
                    break;

                case SimpleOrientation.Rotated90DegreesCounterclockwise:
                    Rotate(90);
                    break;

                case SimpleOrientation.Rotated180DegreesCounterclockwise:
                    Rotate(180);
                    break;

                case SimpleOrientation.Rotated270DegreesCounterclockwise:
                    Rotate(270);
                    break;

                case SimpleOrientation.Faceup:
                    break;

                case SimpleOrientation.Facedown:
                    break;

                default:

                    break;
            }
        });
    }

    private void Rotate(double value)
    {
        MyAnimation.From = Current;
        MyAnimation.To = (360 - value);
        myStoryBoard.Begin();
        Current = MyAnimation.To;
    }
}

XAML代码:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button
        x:Name="TestButton"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Content="button">
        <Button.Projection>
            <PlaneProjection x:Name="Projection" RotationZ="0" />
        </Button.Projection>
    </Button>
    <Grid.Resources>
        <Storyboard x:Name="myStoryBoard">
            <DoubleAnimation
                x:Name="MyAnimation"
                Storyboard.TargetName="Projection"
                Storyboard.TargetProperty="RotationZ"
                Duration="0:0:1" />
        </Storyboard>
    </Grid.Resources>
</Grid>