[英]How do you animate a property on a custom control in UWP?
我想在自定義控件中為畫筆設置動畫。 畫筆的不透明度像這樣綁定到畫筆上:
<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)上設置動畫屬性?
如何在 UWP 中為自定義控件上的屬性設置動畫?
我們無法使用 Composition api 來動畫自定義屬性。 對於您的要求,更好的方法是使用Storyboard
使用MaskOpacity
值對特定的 Border 控件進行動畫MaskOpacity
。 請參考以下代碼。
public static Border RootBorder { get; set; }
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
// give the border x:Name RootBorder.
RootBorder = GetTemplateChild("RootBorder") as Border;
}
private static void OnValuePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var duration = new Duration(TimeSpan.FromSeconds(10));
var ease = new CubicEase { EasingMode = EasingMode.EaseOut };
var animation = new DoubleAnimation
{
EasingFunction = ease,
Duration = duration
};
var conStoryboard = new Storyboard();
conStoryboard.Children.Add(animation);
animation.From = 1.0f;
animation.To = (double)dependencyObject.GetValue(MaskOpacityProperty);
animation.EnableDependentAnimation = false;
Storyboard.SetTarget(animation, RootBorder);
Storyboard.SetTargetProperty(animation, "Opacity");
conStoryboard.Begin();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.