Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用故事板沿最短的';路线';_C#_Wpf_Animation_Storyboard_Rotatetransform - Fatal编程技术网

C# 使用故事板沿最短的';路线';

C# 使用故事板沿最短的';路线';,c#,wpf,animation,storyboard,rotatetransform,C#,Wpf,Animation,Storyboard,Rotatetransform,我正在开发一个“类似指南针”的控件,它可以沿0-360度的值旋转;完整的圆圈。通过在ViewModel中设置特性,栅格将设置新角度的动画。 但是,当动画从10度旋转到350度时,动画不会使用到新角度的最短路径。它几乎在整圈内顺时针旋转 但我想实现的是,它沿着最短的路径进行动画:从10到350应该逆时针进行动画,仅为20度 下面我添加了一个虚构的代码示例,但它没有绑定到ViewModel中的属性,而是使用绑定到ListBox(复制粘贴应该可以让它运行)。在列表框中选择项目会旋转网格(其中包含“指针

我正在开发一个“类似指南针”的控件,它可以沿0-360度的值旋转;完整的圆圈。通过在ViewModel中设置特性,栅格将设置新角度的动画。 但是,当动画从10度旋转到350度时,动画不会使用到新角度的最短路径。它几乎在整圈内顺时针旋转

但我想实现的是,它沿着最短的路径进行动画:从10到350应该逆时针进行动画,仅为20度

下面我添加了一个虚构的代码示例,但它没有绑定到ViewModel中的属性,而是使用绑定到ListBox(复制粘贴应该可以让它运行)。在列表框中选择项目会旋转网格(其中包含“指针”)

如果可能,我更喜欢“所有XAML”解决方案。我想达到的目标是否可行

<Window x:Class="CompassRotation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="375" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>

        <Grid>
            <Ellipse Width="300" Height="300" Fill="DarkCyan" />
            <Label Content="{Binding SelectedItem.Content, ElementName=Angle, UpdateSourceTrigger=Explicit}"
                   FontSize="36" />
            <Grid Width="300" Height="300"
                  RenderTransformOrigin="0.5,0.5"
                  Tag="{Binding SelectedItem.Content, ElementName=Angle, UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}">
                <Grid.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="(Grid.RenderTransform).(RotateTransform.Angle)"
                                    Duration="0:0:1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Grid.Triggers>

                <Grid.RenderTransform>
                    <RotateTransform Angle="{Binding SelectedItem.Content, ElementName=Angle, FallbackValue=45}" />
                </Grid.RenderTransform>

                <Rectangle Width="15" Height="300">
                    <Rectangle.Fill>
                        <LinearGradientBrush>
                            <GradientStopCollection>
                                <GradientStop Color="Red" Offset="0" />
                                <GradientStop Color="Green" Offset="1" />
                            </GradientStopCollection>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </Grid>
        </Grid>
        <ListBox Grid.Column="1" Name="Angle">
            <ListBoxItem>0</ListBoxItem>
            <ListBoxItem>45</ListBoxItem>
            <ListBoxItem>90</ListBoxItem>
            <ListBoxItem>135</ListBoxItem>
            <ListBoxItem>180</ListBoxItem>
            <ListBoxItem>225</ListBoxItem>
            <ListBoxItem>270</ListBoxItem>
            <ListBoxItem>305</ListBoxItem>
            <ListBoxItem>360</ListBoxItem>
        </ListBox>
    </Grid>
</Window>

0
45
90
135
180
225
270
305
360

A
DoubleAnimation
有一个可绑定到的,用于设置动画的起始值

因此,在逆时针将ViewModel的
角度
值从10更新为350之前,请将ViewModel的
from
值设置为370,动画将从370(=10)度标记逆时针滚动回350

这就留下了一个问题:如果要将
角度
从值
更新为
时,如何确定要设置的
的值

我们知道的一件事是,要设置的
From
属性应该在
[到-180,到+180]
的间隔内,以确保旋转采用到
的最短路径

如果我们把这个区间的下限称为-180,那么

From = lowerbound + (from - lowerbound) % 360
或者实际上,因为,对于
[-360360]
范围内的
角度,更安全的公式是

From = lowerbound + (from - lowerbound + 720) % 360

DoubleAnimation
具有一个可绑定到的设置动画起始值的

因此,在逆时针将ViewModel的
角度
值从10更新为350之前,请将ViewModel的
from
值设置为370,动画将从370(=10)度标记逆时针滚动回350

这就留下了一个问题:如果要将
角度
从值
更新为
时,如何确定要设置的
的值

我们知道的一件事是,要设置的
From
属性应该在
[到-180,到+180]
的间隔内,以确保旋转采用到
的最短路径

如果我们把这个区间的下限称为-180,那么

From = lowerbound + (from - lowerbound) % 360
或者实际上,因为,对于
[-360360]
范围内的
角度,更安全的公式是

From = lowerbound + (from - lowerbound + 720) % 360

我怀疑您将需要一些代码隐藏或查看模型代码。我怀疑您将需要一些代码隐藏或查看模型代码。