从C#代码

时间:2016-01-15 16:24:59

标签: c# wpf animation

我试图调用C#中的xaml代码声明的故事板:

<Storyboard x:Key="BotRotation"  Duration="00:00:4" RepeatBehavior="Forever" >
            <DoubleAnimation BeginTime="0:0:0"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="1" To="-1"
            Duration="0:0:2"   
            />
            <DoubleAnimation
            BeginTime="0:0:2"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="-1" To="1"
            Duration="0:0:2"  
            />
        </Storyboard>
</Window.Resources>

此故事板应修改图像的ScaleX属性。 图像声明:

<Image Name="uiRobotIcon" Height="64" Width="64" Source="/YoutubeTelegramAudio;component/imgs/ic_robot.png" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1" ScaleX="1" x:Name="imageRotateTransformm" />
                    <SkewTransform AngleY="0" AngleX="0" />
                    <RotateTransform Angle="0" />
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>

然后,当我点击另一个按钮时,我想从C#开始这个动画。 感谢。

4 个答案:

答案 0 :(得分:2)

这很容易。只需找到while (true) { Task.Run(() =>{ var request = WebRequest.Create("url1"); request.GetResponse(); }); Task.Run(() =>{ var request = WebRequest.Create("url2"); request.GetResponse(); }); Thread.Sleep(15000); } ,将其投放到Resource,然后调用其Begin()方法:

Storyboard

将上面的代码放在某个事件处理程序中,它应该可以正常工作。

可是:

在应用程序中声明Storyboard的位置也很重要。如果它在Storyboard sb = (<YourNamespace>.Properties.Resources["BotRotation"] as Storyboard); sb.Begin(); ,那么不用担心,而是在其他任何地方,这可能是无法访问的。但我认为App.xaml标记也可以包含可访问的资源。我现在无法测试它:)

答案 1 :(得分:1)

您也可以在纯xaml中执行此操作,只需使用触发操作:

    <Button>
        <Button.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <BeginStoryboard  Storyboard="{DynamicResource BotRotation}"/>
            </EventTrigger>
        </Button.Triggers>
    </Button>

答案 2 :(得分:0)

我找到了解决方案。 首先,我将storyboard标签包含在BeginStoryBoard中:

<Window.Resources>
    <BeginStoryboard x:Key="BotRotation">
        <Storyboard   Duration="00:00:4" RepeatBehavior="Forever" >
            <DoubleAnimation BeginTime="0:0:0"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="1" To="-1"
            Duration="0:0:2"   
            />
            <DoubleAnimation
            BeginTime="0:0:2"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="-1" To="1"
            Duration="0:0:2"  
            />
        </Storyboard>
    </BeginStoryboard>
</Window.Resources>

最后,我使用以下语句从C#调用动画:

BeginStoryboard sb = this.FindResource("BotRotation") as BeginStoryboard;
sb.Storyboard.Begin();

答案 3 :(得分:0)

我在代码项目中找到了一个解决方案: https://www.codeproject.com/Articles/364529/Animation-using-Storyboards-in-WPF

首先添加标签:

现在您可以创建故事板:

     <Storyboard x: Key = "yourSb">
         <DoubleAnimation Storyboard.TargetName = "_ window"
       Storyboard.TargetProperty = "Opacity" From = "1" To = "0.3"
       Duration = "0: 0: 1" />
     </Storyboard>

别忘了在StoryBoard标记的开头添加'x:key =“ yourSb”'。

在您的代码中:

       Storyboard s = (Storyboard)TryFindResource("yourSb");
        s.Begin();