2009-12-28 102 views
3

開始一個故事板動畫嘛,正如標題:XAML屬性負載

我有一個故事板,我想這是動畫開始播放而無需代碼干預。 這個要求的原因是我針對的是Silverlight Embedded,而我現在懶得重新編譯我的應用程序。而且,想到這一點,僅在將來改變動畫會更容易。

XAML是否有屬性可以在xaml加載後立即運行故事板?

回答

14

可以使用Loaded事件開始你的故事板

查看MSDN中的示例: Storyboard (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> 

對象矩形的屬性。在觸發器屬性中,我們定義了一個EventTrigger,它將在發生此事件時觸發。我們選擇Rectangle.Loaded事件,這意味着它將在加載時觸發;)。

我們添加一個BeginStoryboard屬性來開始我們的故事板,並添加一個Storyboard。該動畫將在不透明度屬性上使用DoubleAnimation,這意味着在5秒的持續時間內,不透明度將逐漸消失爲零,並返回(AutoReverse屬性),並執行Forever(RepeatBehaviour屬性)。

+0

我直接去了沒有檢查MSDN(對我感到羞恥)。 從MSDN看來,沒有什麼像我期待的行爲。故事板必須從代碼開始...... 您的建議仍然涉及編寫/更改代碼。 – Shaihi 2009-12-28 09:53:00

+0

如果XAML可以被視爲代碼,那麼是的:)你將不得不寫一點XAML才能使它工作;) – Arcturus 2009-12-28 10:09:01

+0

好的。我一定是很厚:(我會閱讀如何在XAML中使用事件,並回來以防它解決了我的問題,謝謝 – Shaihi 2009-12-28 10:16:04

2
<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> 
+2

解釋會有幫助.. – 2012-11-29 07:08:08