Why isn't my windows phone silverlight rotatio

2019-09-14 22:46发布

问题:

I've got this in my XAML:

            <Grid.Resources>
                <Storyboard x:Name="Storyboard_Animation">
                    <DoubleAnimation 
                            Storyboard.TargetName="button_Submit" 
                            Storyboard.TargetProperty="Angle" 
                            From="0" 
                            To="360" 
                            Duration="0:0:1"></DoubleAnimation>
                </Storyboard>
            </Grid.Resources>

I have the button in the same Grid:

            <Button Grid.Row="0" Grid.Column="1" Content="Submit" Margin="0" Name="button_Submit" Click="button_Submit_Click">
                <Button.Template>
                    <ControlTemplate>
                        <Image Source="Images/buttonImage.png"></Image>
                    </ControlTemplate>
                </Button.Template>
                <Button.RenderTransform>
                    <RotateTransform></RotateTransform>
                </Button.RenderTransform>
            </Button>

I have this in my click method:

    private void button_Submit_Click(object sender, RoutedEventArgs e)
    {
        Storyboard_Animation.Begin();
    }

When I click on my button I get the error: Cannot resolve TargetProperty Angle on specified object.

But, I have no idea what I'm supposed to use other than Angle.

I have this other piece of code that works fine:

    private void RotateStar()
    {
        button_Submit.RenderTransformOrigin = new Point(0.5, 0.5);
        button_Submit.RenderTransform = new RotateTransform();
        DoubleAnimation da = new DoubleAnimation
        {
            From = 0,
            To = 360,
            Duration = TimeSpan.FromSeconds(0.3)
        };
        Storyboard.SetTarget(da, button_Submit.RenderTransform);
        Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(da);
        sb.Begin();
    }

I'd like to put the storyboard in the XAML instead of in code. What do I need to add/change in my XAML version so that it works like the code version?

回答1:

Try this:

<Grid.Resources>
    <Storyboard x:Name="Storyboard_Animation">
        <DoubleAnimation 
            Storyboard.TargetName="button_Submit" 
            Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
            From="0" 
            To="360" 
            Duration="0:0:1">
        </DoubleAnimation>
    </Storyboard>
</Grid.Resources>


回答2:

Your problem is in incorrect using "TargetProperty". Button doesn't have Angle property, you should use it for RenderTransform.

Such this:

 <Storyboard x:Name="Storyboard_Animation">
                                <DoubleAnimation Duration="0:0:1" To="-180.221" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="button" d:IsOptimized="True"/>
   </Storyboard>

Regards, Roman.