用于在加载时启动故事板动画的 XAML 属性

发布于 2024-08-16 04:24:27 字数 172 浏览 5 评论 0原文

好吧,正如标题所示:

我有一个故事板,我希望它的动画在没有代码干预的情况下开始。 提出此要求的原因是我的目标是 Silverlight Embedded,而且我现在也懒得重新编译我的应用程序。而且,想来,以后只改变动画会更容易一些。

XAML 是否具有使故事板在 Xaml 加载后立即运行的属性?

Well, as the title suggests:

I have a storyboard and I want it's animation to start without the intervention of code.
The reason for this requirement is that I am targeting Silverlight Embedded and I am too lazy right now to recompile my application as well. And, coming to think of it, it will be easier to change the animation only in the future.

Does XAML have a property to make the storyboard run as soon as the xaml loads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2024-08-23 04:24:27

您可以使用 Loaded 事件来启动情节提要

请参阅 MSDN 有关示例:
故事板 (Silverlight)

从MSDN:

<Canvas
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100"
    Height="100"
    Fill="Blue">
    <Rectangle.Triggers>

      <!-- Animates the rectangle's opacity.
           This is the important part, the EventTrigger which will start our animation -->

      <EventTrigger RoutedEvent="Rectangle.Loaded">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Storyboard.TargetName="MyAnimatedRectangle"
              Storyboard.TargetProperty="Opacity"
              From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Rectangle.Triggers>
  </Rectangle>
</Canvas>

对象 Rectangle 有属性。在 Triggers 属性中,我们定义了一个 EventTrigger,当该事件发生时它将触发。我们选择 Rectangle.Loaded 事件,这意味着它将在加载时触发;)。

我们添加一个 BeginStoryboard 属性来开始我们的故事板,并添加一个故事板。此动画将在 Opacity 属性上使用 DoubleAnimation,这意味着在 5 秒的持续时间内,不透明度将逐渐淡入零,然后返回(AutoReverse 属性),并且它将永远执行此操作(RepeatBehaviour 属性)。

You can use the Loaded event to start your storyboard

See MSDN for an example:
Storyboard (Silverlight)

Picked the example from MSDN:

<Canvas
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100"
    Height="100"
    Fill="Blue">
    <Rectangle.Triggers>

      <!-- Animates the rectangle's opacity.
           This is the important part, the EventTrigger which will start our animation -->

      <EventTrigger RoutedEvent="Rectangle.Loaded">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Storyboard.TargetName="MyAnimatedRectangle"
              Storyboard.TargetProperty="Opacity"
              From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Rectangle.Triggers>
  </Rectangle>
</Canvas>

The object Rectangle has properties. In the Triggers property we defined an EventTrigger which will fire when this event will occur. We choose the Rectangle.Loaded event, which means it will fire when loaded ;).

We add a BeginStoryboard property to begin our storyboard, and add a Storyboard. This animation will use a DoubleAnimation on the Opacity property, which means that in a duration of 5 seconds, the opacity will gradually fade to zero, and back (AutoReverse property) and it will do this Forever (the RepeatBehaviour property).

苍风燃霜 2024-08-23 04:24:27
<UserControl x:Class="SOSMVVM.AniM11"
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>


    <StackPanel Margin="5">
        <Rectangle Name="rect3" Fill="Blue" Margin="2" Width="20"
      Height="20" HorizontalAlignment="Left" />
        <Button Margin="2,20,0,0" HorizontalAlignment="Left"
      Content="Start Animations" Width="100">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>

                                <DoubleAnimation
                  Storyboard.TargetName="rect3" Storyboard.TargetProperty="Width"
                  From="20" To="400" Duration="0:0:10" SpeedRatio="0.5" />


                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </StackPanel>


</UserControl>
<UserControl x:Class="SOSMVVM.AniM11"
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>


    <StackPanel Margin="5">
        <Rectangle Name="rect3" Fill="Blue" Margin="2" Width="20"
      Height="20" HorizontalAlignment="Left" />
        <Button Margin="2,20,0,0" HorizontalAlignment="Left"
      Content="Start Animations" Width="100">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>

                                <DoubleAnimation
                  Storyboard.TargetName="rect3" Storyboard.TargetProperty="Width"
                  From="20" To="400" Duration="0:0:10" SpeedRatio="0.5" />


                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </StackPanel>


</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文