0

I'm new to XAML. I wrote following routine to rotate an image by a given angle (0 to 360). I put in a slider control to set the angle based on slider value. Works great! However, when running the program and clicking the 'spin' button,a For/Next loop goes from 0 to 360 and the image will only display the last angle rotation (360). I did put in a Sleep command to slow, just in case I wasn't catching the previous updates. Any help with why it won't update continuously would be greatly appreciated. Thank you.

Imports System.Threading.Thread
Imports System.Windows.Media.Imaging.BitmapImage

Class MainWindow

    Private Sub Slider1_ValueChanged(sender As System.Object, e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles Slider1.ValueChanged
' ---- when I adjust manually, this works perfectly
        Dim rotateTransform1 As New RotateTransform
        rotateTransform1.Angle = Slider1.Value
        lblAngle.Content = rotateTransform1.Angle
        Image1.RenderTransform = rotateTransform1
    End Sub

    Private Sub btnSpin_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSpin.Click
    Dim spinAngle as Double
    For SpinAngle 0 to 360
            spinWheel(spinAngle)
            Sleep(50)
        Next spinAngle
    End Sub

    Private Sub spinWheel(ByVal spinAngle)
        Dim rotateTransform1 As New RotateTransform
        rotateTransform1.Angle = SpinAngle 'Slider1.Value
       Image1.RenderTransform = rotateTransform1
        lblAngle.Content = rotateTransform1.Angle
        Image1.InvalidateVisual()

    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' Image1.createOption = BitmapCreateOptions.IgnoreImageCache

    End Sub

End Class

XAML

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="677" Width="910">
    <Grid Background="#FF006903">
        <Grid.RowDefinitions>
            <RowDefinition Height="238*" />
            <RowDefinition Height="178*" />
        </Grid.RowDefinitions>
        <Button Content="SPIN!" Height="23" HorizontalAlignment="Left" Margin="521,101,0,0" Name="btnSpin" VerticalAlignment="Top" Width="75" Grid.Row="1" FontFamily="Tahoma" FontSize="15" FontWeight="ExtraBold" />
        <Image Height="615" Margin="32,7,0,0" Name="Image1" Stretch="None" VerticalAlignment="Top"
               RenderTransformOrigin=" 0.5,0.5"    Source="/rotatePicture;component/Images/purp_wheel_cropped.png"
               Grid.RowSpan="2" HorizontalAlignment="Left" Width="619" />

        <Slider Height="25" HorizontalAlignment="Left" Margin="127,188,0,0" Name="Slider1" VerticalAlignment="Top" Width="350" Maximum="360" Grid.Row="1" />
        <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="521,175,0,0" Name="lblAngle" VerticalAlignment="Top" Width="75" Grid.Row="1" FontFamily="Tahoma" FontSize="15" FontWeight="ExtraBold" />
        <Image Height="29" HorizontalAlignment="Left" Margin="535,252,0,0" Name="Image2" Stretch="Fill" VerticalAlignment="Top" Width="55" Source="/rotatePicture;component/Images/wheel_pointer.png" />
    </Grid>
</Window>
4

2 回答 2

1

您的方法的问题是您通过重复调用Click 处理程序来阻塞 UI 线程。SleepWPF 为您尝试做的事情提供了一种非常优雅的机制。它被称为动画

动画 a 的旋转的方法FrameworkElement在 C# 中可能如下所示(抱歉,我不会说 VB)。

private void RotateElement(
    FrameworkElement element, double from, double to, TimeSpan duration)
{
    var transform = element.RenderTransform as RotateTransform;
    if (transform != null)
    {
        var animation = new DoubleAnimation(from, to, duration);
        transform.BeginAnimation(RotateTransform.AngleProperty, animation);
    }
}

请注意,RotateTransform必须已经包含在 的RenderTransform属性中FrameworkElement。例如,它可以像这样在 XAML 中分配:

<Image RenderTransformOrigin="0.5,0.5" ...>
    <Image.RenderTransform>
        <RotateTransform/>
    </Image.RenderTransform>
</Image>

您将在 Button Click 处理程序中调用该RotateElement方法,如下所示:

private void btnSpin_Click(object sender, RoutedEventArgs e)
{
    RotateElement(Image1, 0, 360, TimeSpan.FromMilliseconds(360 * 50));
}

另请注意,您Slider1_ValueChanged也不必RotateTransform每次都创建一个新的。

此外,很少需要调用InvalidateVisual,就像您在 中所做的那样spinWheel

于 2013-02-08T08:47:55.117 回答
0

克莱门斯的回答很好。不要忘记您可以暂停/恢复动画,但前提是您在代码隐藏中创建它们。如果我没记错的话,如果你在 XAML 中启动它们,你就无法控制动画。

于 2013-02-09T12:34:07.407 回答