【问题标题】:Animate MatrixAnimationUsingPath not running on runtime in WPFAnimate MatrixAnimationUsingPath 未在 WPF 中的运行时运行
【发布时间】:2021-07-21 15:54:55
【问题描述】:

我的箭头和线条与我的另一个 SO question 一起工作,用于原型/概念证明。现在,我想使用相同的箭头应用于包含超过 5 行的实际应用程序。所以,我想从代码转换矩阵并在按钮单击时启动情节提要。不幸的是,动画没有开始(箭头在第一个位置并且没有在路径数据中移动

我有来自MSFT Path Animations Overview的样本。

以下是我当前的应用程序

XAML:

<Path
    x:Name="Path_Room1"
    Width="68.729"
    Height="71.077"
    Data="M248.37977,129.8855 C248.37977,129.8855 248.37977,102.93943 248.37977,97.931796 248.37977,92.924162 253.51313,93.863426 263.68489,93.863426 273.85665,93.863426 274.63926,94.685097 274.63926,86.939011 274.63926,76.767263 274.63926,65.520282 274.63926,62.46878 274.63926,59.417279 277.53466,59.827827 280.50799,59.827827 283.48133,59.827827 316.10902,59.827827 316.10902,59.827827"
    Stretch="Fill"
    Stroke="Black" />
<Path
    x:Name="Arrowhead"
    Width="23"
    Height="19.5"
    HorizontalAlignment="Left"
    VerticalAlignment="Top"
    Data="M94,29 L117,39 95,49 99,38 z"
    Fill="#ff387cc0"
    Stretch="Fill">
    <Path.RenderTransform>
        <MatrixTransform />
    </Path.RenderTransform>
</Path>

代码隐藏:

private void BtnRoom1_Click(object sender, RoutedEventArgs e)
{
    var pathdata = Path_Room1.Data;
    // I have to comment this as other animation is failed to found settargetname
    //NameScope.SetNameScope(this, new NameScope());
    PathGeometry animationPath = PathGeometry.CreateFromGeometry(pathdata);
    animationPath.Freeze();
    System.Windows.Media.Animation.MatrixAnimationUsingPath matrixAnimationUsingPath = new System.Windows.Media.Animation.MatrixAnimationUsingPath();
    matrixAnimationUsingPath.PathGeometry = animationPath;
    matrixAnimationUsingPath.Duration = TimeSpan.FromSeconds(5);
    matrixAnimationUsingPath.RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever;
    matrixAnimationUsingPath.DoesRotateWithTangent = true;

    System.Windows.Media.Animation.Storyboard.SetTarget(matrixAnimationUsingPath, Arrowhead);
    System.Windows.Media.Animation.Storyboard.SetTargetProperty(matrixAnimationUsingPath, new PropertyPath("RenderTransform.Matrix"));

    System.Windows.Media.Animation.Storyboard pathAnimationStoryboard = new System.Windows.Media.Animation.Storyboard();
    pathAnimationStoryboard.Children.Add(matrixAnimationUsingPath);

    Arrowhead.Loaded += delegate (object ss, RoutedEventArgs es)
    {
        pathAnimationStoryboard.Begin(this);
    };
}

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    将处理程序附加到Arrowhead 元素的Loaded 事件如果已加载,则无效。

    除此之外,您不需要在后面的代码中使用情节提要。

    private void BtnRoom1_Click(object sender, RoutedEventArgs e)
    {
        var pathdata = Path_Room1.Data;
        var animationPath = PathGeometry.CreateFromGeometry(pathdata);
        animationPath.Freeze();
    
        var matrixAnimationUsingPath = new MatrixAnimationUsingPath
        {
            PathGeometry = animationPath,
            Duration = TimeSpan.FromSeconds(5),
            RepeatBehavior = RepeatBehavior.Forever,
            DoesRotateWithTangent = true
        };
    
        Arrowhead.RenderTransform.BeginAnimation(
            MatrixTransform.MatrixProperty,
            matrixAnimationUsingPath);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多