2014-01-20 83 views
0

將問題移至資源字典以便在我的應用程序中使用時遇到問題。Reasource字典和故事板

基本上想要在多個用戶控件上使用相同的故事板。

資源Dicitionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Storyboard RepeatBehavior="Forever" x:Key="AnimateWidth"> 
<DoubleAnimation Storyboard.TargetProperty="Width" From="1" To="250" Duration="0:0:2" BeginTime="0:0:0" FillBehavior="Stop" /> 
</Storyboard> 
</ResourceDictionary> 

用戶控件

<UserControl x:Class="Toolset.WMIControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:View="clr-namespace:Toolset" 
      Name="WmiControl_UserControl" 
      Loaded="UserControl_Loaded" 

      mc:Ignorable="d" 
      > 
<UserControl.Resources> 
     <ResourceDictionary x:Key="Animations"> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="AnimationStyles.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 


    </UserControl.Resources> 

<DockPanel Name="DockPanel1" Style="{StaticResource Animations}"> 
      <DockPanel.Triggers> 
      <EventTrigger RoutedEvent="Loaded"> 

       <BeginStoryboard Storyboard="{Binding AnimateWidth, Source={StaticResource Animations}}"/> 
      </EventTrigger> 
     </DockPanel.Triggers> 
    </DockPanel> 

不是真的知道在哪裏我已經錯在這裏。任何幫助,將不勝感激。 得到錯誤「必須有一個Storyboard對象引用在此之前觸發動作可以執行。

+0

在'BeginStoryboard'嘗試使用'StaticResource'這樣的:'''在ResourceDictionary'鍵是不需要,'x:Key =「動畫」'必須刪除。 –

回答

0
<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="AnimationStyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 

<DockPanel Name="DockPanel1"> 
    <DockPanel.Triggers> 
     <EventTrigger RoutedEvent="Loaded"> 
      <BeginStoryboard Storyboard="{StaticResource AnimateWidth}"/> 
     </EventTrigger> 
    </DockPanel.Triggers> 
</DockPanel> 
+0

找不到名爲'AnimateWidth'的資源。資源名稱區分大小寫另外,我必須命名資源字典,因爲它說每個資源字典都必須有一個密鑰 –

+0

''''''''''''''''''所以''UserControl'的資源也被'DockPanel'使用。 – Sankarann

0

回答我自己的問題..

這是你如何使用動畫從資源字典是在另一個文件中。 如果不使用C#,因爲你的不定型的觀點保持它在視圖

<ResourceDictionary x:Key="Animations"> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="AnimationStyles.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 

     <DockPanel x:Name="Simple_Mode" DockPanel.Dock="Top" 
        HorizontalAlignment="Left" 
        ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
         Resources="{StaticResource Animations}" 
         > 
       <DockPanel.Triggers> 
        <EventTrigger RoutedEvent="Loaded"> 
         <BeginStoryboard Storyboard="{StaticResource AnimateWidth}"/> 
        </EventTrigger> 
       </DockPanel.Triggers> 
     </DockPanel>