我有一个问题,我创建了一个名为Tile的控件(就像Windows10上的Tile )。此磁贴通过使用投影类模拟3D旋转,就像Silverlight中的投影类一样。
我们有一个基本投影类,如下所示:
abstract public class Projection
: FrameworkElement
{
static public readonly DependencyProperty RotationZProperty = DependencyProperty.Register(
nameof(RotationZ),
typeof(double),
typeof(Projection),
new UIPropertyMetadata(0.0, OnRotationChanged));
static public readonly DependencyProperty RotationYProperty = DependencyProperty.Register(
nameof(RotationY),
typeof(double),
typeof(Projection),
new UIPropertyMetadata(0.0, OnRotationChanged));
static public readonly DependencyProperty RotationXProperty = DependencyProperty.Register(
nameof(RotationX),
typeof(double),
typeof(Projection),
new UIPropertyMetadata(0.0, OnRotationChanged));
public double RotationZ
{
get { return this.GetValue<double>(RotationZProperty); }
set { SetValue(RotationZProperty, value); }
}
public double RotationY
{
get { return this.GetValue<double>(RotationYProperty); }
set { SetValue(RotationYProperty, value); }
}
public double RotationX
{
get { return this.GetValue<double>(RotationXProperty); }
set { SetValue(RotationXProperty, value); }
}
public FrameworkElement Child
{
get;
set;
}
static private void OnRotationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Projection pl)
{
pl.OnRotationChanged();
}
}
private void OnRotationChanged()
{
// Some code
}}
After that, we have the PlaneProjectionClass :
[ContentProperty("Child")]
sealed public class PlaneProjection
: Projection
{
}Tile类使用Projection类型依赖属性:
public class Tile
{
static public readonly DependencyProperty PlaneProjectionProperty = DependencyProperty.Register(
nameof(Projection),
typeof(Projection),
typeof(Tile),
new UIPropertyMetadata());
public Projection Projection
{
get { return (Projection)GetValue(PlaneProjectionProperty); }
private set { SetValue(PlaneProjectionProperty, value); }
}
override public void OnApplyTemplate()
{
Projection = GetTemplateChild("PART_Projection") as Projection;
}
}因此,对于XAML,我们在ControlTemplate中有如下代码:
<controls:PlaneProjection x:Name="PART_PlaneProjection">
<Border>
<Grid>
<!-- Some design -->
</Grid>
</Border>
</controls:PlaneProjection>现在我想要对平面投影进行动画处理。
因此,我创建了故事板,并使用rotationX对投影进行动画处理:
static public void CreateAnimation(Tile tile)
{
Storyboard.SetTarget(anim, tile);
Storyboard.SetTargetProperty(doubleAnim, new PropertyPath("(Tile.Projection).(PlaneProjection.RotationX"));
}但是在调试时,我遇到这样的错误:无法解析属性'(Tile.Projection).(PlaneProjection.RotationX)路径上的所有属性引用
我不明白这个错误:(关于在自定义控件上使用PropertyPath有什么想法吗?
发布于 2017-07-19 03:53:33
类Tile中的Projection属性不遵循依赖属性的命名约定。
例如,它应该看起来像这样:
public static readonly DependencyProperty PlaneProjectionProperty =
DependencyProperty.Register(nameof(PlaneProjection), typeof(Projection), typeof(Tile));
public Projection PlaneProjection
{
get { return (Projection)GetValue(PlaneProjectionProperty); }
private set { SetValue(PlaneProjectionProperty, value); }
}属性路径应该是这样的:
Storyboard.SetTargetProperty(anim, new PropertyPath("PlaneProjection.RotationX"));你甚至不需要一个故事板。只要打个电话
tile.PlaneProjection.BeginAnimation(Projection.RotationXProperty, anim);需要注意的是,私有setter不会使依赖属性成为只读的。详情请参见Read-Only Dependency Properties。
https://stackoverflow.com/questions/45175683
复制相似问题