我已经为一个元素创建了一个自定义类,它继承自UWP中的Path
类(箭头绘图),现在它似乎工作正常,但当我试图将其中的两个元素放在屏幕上时,其中一个不会呈现。我首先初始化的那个是将要呈现的那个,我认为它可能与类示例引用一些在它们之间共享的指针或类似的东西有关,但我还没有找到解决方案。
下面是我编写的自定义类的代码:
public sealed partial class TimePickerArrow : Path
{
#region Arrow Direction XAML Property
public enum Directions
{
Up = 0,
Down = 180,
Left = -90,
Right = 90
};
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register("Direction", typeof(Directions), typeof(TimePickerArrow), new PropertyMetadata(Directions.Up, OnArrowDirectionChanged));
public Directions Direction
{
get { return (Directions)GetValue(DirectionProperty); }
set { SetValue(DirectionProperty, value); }
}
#endregion
#region Arrow Hover Effect Fill Xaml Property
public static readonly DependencyProperty HoverFillProperty =
DependencyProperty.Register("HoverFill", typeof(Brush), typeof(TimePickerArrow), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));
public Brush HoverFill
{
get { return (Brush)GetValue(HoverFillProperty); }
set { SetValue(HoverFillProperty, value); }
}
#endregion
// static arrow color set by the Fill property of the path,
// used to save the original arrow fill color that gets changed on hover
Brush StaticFill;
public TimePickerArrow()
{
#region PATH PROPERTIES INIT
this.Direction = Direction;
this.HoverFill = HoverFill;
this.Fill = new SolidColorBrush(Colors.Black);
this.StaticFill = Fill;
#endregion
InitArrowPath();
this.PointerEntered += TimePickerArrow_HoverEntered;
this.PointerExited += TimePickerArrow_HoverExited;
}
private void InitArrowPath()
{
this.Style = Application.Current.Resources["TimePickerArrowPath"] as Style;
}
private static void OnArrowDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TimePickerArrow arrow = (TimePickerArrow)d;
Directions newArrowDirection = (Directions)e.NewValue;
// Update the RenderTransform based on the new Direction value
CompositeTransform arrowDirectionRotation = new CompositeTransform();
arrowDirectionRotation.Rotation = (double)newArrowDirection;
arrow.RenderTransform = arrowDirectionRotation;
}
private void TimePickerArrow_HoverEntered(object sender, PointerRoutedEventArgs e)
{
this.Fill = HoverFill;
}
private void TimePickerArrow_HoverExited(object sender, PointerRoutedEventArgs e)
{
this.Fill = StaticFill;
}
}
下面是InitPath()方法中引用的样式:
<Style x:Key="TimePickerArrowPath" TargetType="Path">
<Setter Property="Data" Value="M32 6 L58 32 L48.07142857142857 41.92857142857143 L32 25.857142857142854 L16 41.92857142857143 L6 31.92857142857143 Z"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="Stretch" Value="Fill"/>
</Style>
感谢所有愿意做出贡献的人。
1条答案
按热度按时间x6492ojm1#
我真不敢相信我在这上面浪费了这么多时间,问题就出在这一行:
this.Style = Application.Current.Resources["TimePickerArrowPath"] as Style;
由于它是整个应用程序的静态全局资源,它在所有示例之间共享,并且它是它们共享的唯一thign,因为它是第一个“抓住”样式资源的,所以只有第一个初始化被绘制到屏幕上,我只是试图改变形状并删除样式,这就是我看到的地方,它击中了我。我可以通过在代码中为每个示例创建一个新的Style来解决这个问题
有趣的是,我需要这个样式只是为了初始化Path.Data属性的字符串,而不是搞乱XAML几何