2010-10-25 70 views
3

我想在點擊按鈕揭開序幕的動畫。下面的代碼看起來很合理,但是會引發運行時錯誤。理想情況下,我只想將故事板指向命令。這是可能的(我用Google搜索,並沒有發現任何表明它是)綁定的Silverlight動畫按鈕單擊

   <Button Width="50" Height="24" Content="X"> 
        <Button.Triggers> 
         <EventTrigger RoutedEvent="Button.Click"> 
          <BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}"> 
          </BeginStoryboard> 
         </EventTrigger> 
        </Button.Triggers> 
       </Button> 

這裏的故事板:

<UserControl.Resources> 
    <Storyboard x:Name="ShowTemplateSelector"> 
     <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" /> 
     <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" /> 
    </Storyboard> 

運行時錯誤說:

微軟JScript運行時錯誤:未處理的錯誤在Silverlight Application 2531發生錯誤。 System.Windows.Application.LoadComponent(Object component,Uri resourceLocator) at SilverlightApplication8.MainPage.InitializeComponent() at SilverlightApplication8.MainPage..ctor() at SilverlightApplication8.App.Application_Startup(){Line:97 Position:55]對象發件人,StartupEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate,Object sender,Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex, String eventName)

97行是這樣的:

<EventTrigger RoutedEvent="Button.Click"> 

回答

3

你可以得到正是你想用混合SDK什麼命名範圍將名稱解析。只要把一個ControlStoryboardAction行爲,您的按鈕:

<Button Width="50" Height="24" Content="X"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <ei:ControlStoryboardAction Storyboard="{StaticResource ShowTemplateSelector}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </Button> 

其中i和EI定義如下:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
-1

改變了...

<BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}"> 
</BeginStoryboard> 

這個...

<BeginStoryboard Storyboard="{DynamicResource ShowTemplateSelector}"> 
</BeginStoryboard> 
+0

DynamicResource標記擴展不會在Silverlight中存在。它只在WPF中。 – Stephan 2010-10-25 19:50:08

+0

@Stephan謝謝你的信息......只是看着它,它說如果你從WPF移動到SL移動到一個StaticResource並參考它的方式......這是他在做什麼......嗯。 .. – 2010-10-25 19:51:04

+0

當我說我相信我,我希望這是解決方案。我真的希望MS能夠在SL 4中加入這個。 – Stephan 2010-10-25 19:51:46

2

您需要將您的故事板移動到觸發。如果您嘗試引用它作爲一個靜態資源是無法在的ResourceDictionary中

<EventTrigger RoutedEvent="Button.Click"> 
    <BeginStoryboard> 
    <BeginStoryboard.Storyboard> 
     <Storyboard x:Name="ShowTemplateSelector"> 
     <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" /> 
     <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" /> 
     </Storyboard> 
    </BeginStoryboard.Storyboard> 
</BeginStoryboard> 
+0

謝謝。有沒有*任何*方法讓按鈕在t中引用故事板他資源部分?我的意思是,除了ShowTemplateSelector.Begin();在點擊處理程序中,當然。 – 2010-10-25 19:53:02

+0

此外,這是Silverlight的限制,還是WPF也會導致同樣的錯誤呢? – 2010-10-25 19:53:36

+0

我的解釋有點措辭不足。問題不在於找到故事板。 StaticResource標記擴展將罰款故事板罰款。問題在於'Storyboard.TargetName =「canvasStyleSelected」'無法解析名稱canvasStyleSelected,因爲它不在範圍內。 WPF中的DynamicResource會起作用,因爲它將從執行範圍中查找名稱,但不幸的是該擴展在SL中不存在。這是我發現做你所描述的唯一途徑。 – Stephan 2010-10-26 13:47:11