我正在 UWP 项目中创建一个 XAML 自定义控件,并且我将实现我已经发现可在 WPF 应用程序中使用的相同模式,以便在主 DependencyProperty 更改时一次性编辑控件的属性。
在下面的示例代码中,我展示了当外部用户更改 dp(名为“Color” )时如何更改SolidColorBrushdp(称为“ColorBrush”) 。Color
在 WPF 中,这是我实现的模式(正常工作):
public partial class ColorViewer : UserControl
{
// .ctor and other functions
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set
{
SetValue(ColorProperty, value);
}
}
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(Color), typeof(ColorViewer), new FrameworkPropertyMetadata(OnColorChanged));
public SolidColorBrush ColorBrush
{
get { return (SolidColorBrush)GetValue(ColorBrushProperty); }
set { SetValue(ColorBrushProperty, value); }
}
public static readonly DependencyProperty ColorBrushProperty =
DependencyProperty.Register("ColorBrush", typeof(SolidColorBrush), typeof(ColorViewer), null);
private static void OnColorChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var c = (Color) e.NewValue;
ColorViewer view = source as ColorViewer;
view.UpdateColorProperties(c);
}
private void UpdateColorProperties(Color c)
{
ColorBrush = new SolidColorBrush(c);
// Many other things...
}
}
特别是,我将一个FrameWorkPropertyMetadata(以方法作为参数)传递给设置“颜色”dp。
带着我的巨大(和悲伤)惊喜,我发现FrameworkPropertyMetadataUWP 平台不提供该功能!
如何在 UWP 中获得相同的结果?
HUWWW
相关分类