【问题标题】:Silverlight Animation: Cannot resolve TargetNameSilverlight 动画:无法解析 TargetName
【发布时间】:2011-12-28 02:02:25
【问题描述】:

我遇到了动画问题。首先,我尝试使用 XAML 标记中的 VisualStateManager 制作动画。这在尝试为 Opacity 属性设置动画时有效,但不适用于 Height 属性。然后我想我会通过以编程方式制作动画来尝试一下,这样我就可以更轻松地调试。因此,我不断得到以下信息:

无法解析 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();

那么我的代码有什么问题?

【问题讨论】:

    标签: silverlight xaml animation silverlight-4.0 storyboard


    【解决方案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);
    

    是什么破解了它。

    【讨论】:

      【解决方案2】:

      不确定 TargetName 错误,但我很确定您需要类似的东西 TargetProperty 的 (UIElement.Height)。
      现在无法尝试重现您的问题,所以只是一个疯狂的猜测......

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

      【讨论】:

      • 不幸的是,这没有帮助。实际上,我更喜欢让它在代码而不是 XAML 中工作,因为动画 Opacity(在 XAML 中工作正常)看起来更好,但在这种情况下,我需要处理 Completed 事件,我更喜欢在里面处理它我的自定义控件的类(不在视图模型上)。因此,您知道我为什么会收到该错误吗?
      • 好吧,没有什么能阻止您在控件中处理完成的事件,即使情节提要及其触发器是在 xaml 中定义的。您还可以使用 DoubleAnimationUsingKeyframe 将可见性设置为 Collapsed 并在动画结束时更改按钮的内容属性。在这种情况下,最好的解决方案是利用状态并使您的故事板从一个状态转到另一个状态。一旦你的动画变得更复杂,用代码编写故事板就会变得很麻烦。
      【解决方案3】:

      好的,我终于让它工作了......以下链接很有帮助:

      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 = "-";
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 2017-12-12
        • 2012-02-11
        • 2016-08-03
        • 1970-01-01
        相关资源
        最近更新 更多