2011-11-13 70 views
1

我遇到動畫問題。首先,我嘗試使用XAML標記中的VisualStateManager進行動畫製作。嘗試對Opacity屬性進行動畫製作時,該功能有效,但不支持Height屬性。然後我想我會試着通過編程動畫來試試,這樣我就可以更容易地進行調試。因此,我一直得到以下幾點:Silverlight動畫:無法解析目標名稱

無法解析的TargetName ExplorerBody

我唐諾爲什麼動畫的不透明性工作,但沒有高度,我不知道爲什麼它不能解析的TargetName。結帳我的代碼,也許你可以看到的東西,我不能:

<ControlTemplate TargetType="Controls:ApplicationExplorer">       
     <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderBrush}" 
       BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}" 
       CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CornerRadius}" 
       Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}" 
       Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}" > 

      <Grid x:Name="Root" MinWidth="100"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="30" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 
       <Grid Grid.Row="0" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderBackgroundBrush}"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="10" /> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="40" /> 
        </Grid.ColumnDefinitions> 
        <TextBlock Grid.Column="1" Text="{TemplateBinding Title}" Style="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TitleStyle}" VerticalAlignment="Center" /> 
        <Controls:LayoutToggleButton Grid.Column="2" x:Name="LayoutButton" Cursor="Hand" /> 
       </Grid> 
       <Border x:Name="ExplorerBody" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" 
         Grid.Row="1" HorizontalAlignment="Stretch" BorderThickness="0" > 
        <toolkit:TreeViewDragDropTarget AllowedSourceEffects="Copy" x:Name="treeViewDropTarget" 
              HorizontalAlignment="Left" VerticalAlignment="Top" > 
         <sdk:TreeView x:Name="treeView" ItemsSource="{TemplateBinding Nodes}" Background="Transparent" BorderThickness="0" 
          ItemTemplate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TreeItemTemplate}" 
          HorizontalAlignment="Left" Margin="10 0 0 0" /> 
        </toolkit:TreeViewDragDropTarget> 
       </Border> 
      </Grid> 
      <vsm:VisualStateManager.VisualStateGroups> 
       <vsm:VisualStateGroup x:Name="LayoutGroup"> 
        <vsm:VisualState x:Name="Minimized"> 
         <Storyboard> 
          <DoubleAnimation Storyboard.TargetName="ExplorerBody" Storyboard.TargetProperty="Height" Duration="0:0:0.5" From="1" To="0" /> 
         </Storyboard> 
        </vsm:VisualState> 
        <vsm:VisualState x:Name="Maximized" /> 
       </vsm:VisualStateGroup> 
      </vsm:VisualStateManager.VisualStateGroups> 
     </Border> 
    </ControlTemplate> 

這裏就是我試圖得到它使用C#代碼來完成:

Storyboard storyboard = new Storyboard(); 
    storyboard.SetValue(Storyboard.TargetNameProperty, _explorerBody.Name); 
    storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity")); 
    DoubleAnimation anim = new DoubleAnimation(); 
    anim.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500)); 
    anim.To = 0; 
    storyboard.Children.Add(anim); 
    storyboard.Begin(); 

那麼,什麼是錯我的代碼?

回答

0

好吧,我終於得到它的工作...以下鏈接是有幫助的:

http://mindfusion.eu/Forum/YaBB.pl?board=diaglite_disc;action=display;num=1278055916

這裏是我的,以防有人代碼需要它:

private void SwitchLayoutState(object sender, RoutedEventArgs e) 
    { 
     if (_currentState == ControlLayout.None) 
     { 
      throw new ArgumentException("Control layout cannot be None."); 
     } 

     Duration duration = new Duration(new TimeSpan(0, 0, 0, 0, 400)); 
     Storyboard storyboard = new Storyboard(); 
     storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity")); 
     DoubleAnimation animation = new DoubleAnimation(); 
     storyboard.Children.Add(animation); 
     Storyboard.SetTarget(animation, _explorerBody); 

     if (_currentState == ControlLayout.Maximized) 
     { 
      animation.To = 0; 

      storyboard.Completed += (s, args) => { _explorerBody.Visibility = System.Windows.Visibility.Collapsed; }; 
      storyboard.Begin(); 

      _currentState = ControlLayout.Minimized; 
      _layoutButton.Content = "+"; 
     } 
     else if (_currentState == ControlLayout.Minimized) 
     { 
      _explorerBody.Visibility = System.Windows.Visibility.Visible; 

      animation.To = 1; 
      storyboard.Begin(); 

      _currentState = ControlLayout.Maximized; 
      _layoutButton.Content = "-"; 
     } 
    } 
0

不知道TargetName錯誤,但我敢肯定你需要爲TargetProperty提供類似 (UIElement.Height)的東西。
不能嘗試重現你的問題了,所以只是胡亂猜測......

<DoubleAnimation Storyboard.TargetName="ExplorerBody" Storyboard.TargetProperty="(UIElement.Height)" Duration="0:0:0.5" From="1" To="0" /> 
+0

不幸的是,這並沒有幫助。我實際上更喜歡使它在代碼而不是XAML中工作,因爲動畫'Opacity'(在XAML中工作正常)看起來更好,但在這種情況下,我需要處理'Completed'事件,並且我更喜歡在內部處理它我的自定義控件的類(不在視圖模型上)。因此,你有什麼想法爲什麼我得到這個錯誤? – Kassem

+0

好吧,即使故事板及其觸發器是在xaml中定義的,也不會阻止您在控件中處理完成的事件。 您也可以將可見性設置爲Collapsed,並使用DoubleAnimationUsingKeyframe更改動畫結尾的按鈕的content屬性。 在這種情況下,最好的解決方案是利用國家,讓你的故事板從一種狀態變爲另一種狀態。 只要動畫變得更復雜,將故事板全部寫入代碼就會變得毛茸茸。 –

1

你原來企圖MS文檔匹配 -

Storyboard storyboard = new Storyboard(); 

storyboard.SetValue(Storyboard.TargetNameProperty, _explorerBody.Name); 

storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity")); 
DoubleAnimation anim = new DoubleAnimation(); 

和您的解決方案 -

Storyboard storyboard = new Storyboard(); 
    storyboard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity")); 
    DoubleAnimation animation = new DoubleAnimation(); 
    storyboard.Children.Add(animation); 
    Storyboard.SetTarget(animation, _explorerBody); 

尤其是更換 -

storyboard.SetValue(Storyboard.TargetNameProperty part with - 
    Storyboard.SetTarget(animation, _explorerBody); 

是什麼裂紋它。