【发布时间】:2020-06-18 11:23:49
【问题描述】:
我想为自定义控件中的画笔设置动画。画笔的不透明度与画笔绑定如下:
<Style TargetType="trrfc:CustomControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="trrfc:PriceControl">
<Grid>
<Border Background="Red"
Opacity="{TemplateBinding MaskOpacity"}/>
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
控件非常简单,如下所示:
namespace ThetaRex.Radar.Forms.Controls
{
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media;
public class CustomControl : ContentControl
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value",
typeof(decimal),
typeof(CustomControl),
new PropertyMetadata(default(decimal), new PropertyChangedCallback(CustomControl.OnValuePropertyChanged)));
public static readonly DependencyProperty MaskBackgroundProperty = DependencyProperty.Register(
nameof(CustomControl.MaskBackground),
typeof(Brush),
typeof(CustomControl),
new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MaskOpacityProperty = DependencyProperty.Register(
nameof(CustomControl.MaskOpacity),
typeof(float),
typeof(CustomControl),
new PropertyMetadata(default(float)));
public CustomControl()
{
// This allows the control to pick up a template.
this.DefaultStyleKey = typeof(CustomControl);
}
public decimal Value
{
get => (decimal)this.GetValue(CustomControl.ValueProperty);
set => this.SetValue(CustomControl.ValueProperty, value);
}
public Brush MaskBackground
{
get => this.GetValue(CustomControl.MaskBackgroundProperty) as Brush;
set => this.SetValue(CustomControl.MaskBackgroundProperty, value);
}
public float MaskOpacity
{
get => (float)this.GetValue(CustomControl.MaskOpacityProperty);
set => this.SetValue(CustomControl.MaskOpacityProperty, value);
}
private static void OnValuePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
CustomControl priceControl = dependencyObject as CustomControl;
var priceControlVisual = ElementCompositionPreview.GetElementVisual(priceControl);
var compositor = priceControlVisual.Compositor;
// Start a new animation that will flash an opaque background, then slowly fade to transparent.
var opacityAnimation = compositor.CreateScalarKeyFrameAnimation();
opacityAnimation.Duration = TimeSpan.FromSeconds(10.0);
opacityAnimation.InsertKeyFrame(0.0f, 1.0f, compositor.CreateLinearEasingFunction());
opacityAnimation.InsertKeyFrame(1.0f, 0.0f, compositor.CreateLinearEasingFunction());
priceControlVisual.StartAnimation(nameof(priceControl.MaskOpacity), opacityAnimation);
}
}
}
当我运行这个时,我得到一个错误:
The parameter is incorrect. The specified property was not found or cannot be animated. Context: MaskOpacity Expression: MaskOpacity Start Position: 0, End Position: 11
如果我通过在 StartAnimation 调用中指定 priceControl.Opacity 为整个控件的不透明度设置动画,则此代码将按预期工作。有没有人有示例或线索如何为 MaskOpacity 等自定义控件上的属性设置动画?
【问题讨论】:
-
我试过这样做一次。据我所知,您无法使用 Composition API 为自定义属性设置动画。
-
查看表情动画。您可以将某些东西组合在一起,在某些虚拟控件上为属性设置动画,然后表达式为您的自定义属性设置动画。