【发布时间】:2019-06-01 00:41:01
【问题描述】:
我正在一个 UWP 项目中创建一个 XAML 自定义控件,并且我将实现我已经发现可在 WPF 应用程序中使用的相同模式,以便在主 DependencyProperty 更改时一次性编辑控件的属性。
在下面的示例代码中,我展示了当外部用户更改 Color dp(名为“Color”)时如何更改 SolidColorBrush dp(称为“ColorBrush”)。
在 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。
令我大吃一惊的是,我发现 FrameworkPropertyMetadata 在 UWP 平台中不可用!
如何在 UWP 中获得相同的结果?
感谢您的关注。
最好的问候
【问题讨论】:
标签: c# uwp dependency-properties