如何更改 UWP 中另一个 DependencyProperty 值,FrameworkPropertyMetadata 不可用

How to change a DependencyProperty value from another one in UWP, FrameworkPropertyMetadata not being available

我正在 UWP 项目中创建一个 XAML 自定义控件,我打算实现我已经发现可在 WPF 应用程序中使用的相同模式,以便在主 DependencyProperty 更改时立即编辑控件的所有属性.

在下面的示例代码中,我展示了当 Color dp(名为 "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(方法作为其参数)传递给设置 "Color" dp.

令我大吃一惊的是,我发现 FrameworkPropertyMetadata 在 UWP 平台中不可用!

如何在 UWP 中获得相同的结果?

感谢您的关注。

此致

它没有那个,因为恕我直言,它比任何东西都更膨胀, Uwp 有一个更简单的 ProperyMetadata class,它允许您设置 dp 的默认值,如果您想更深入,它还允许您设置在值更改时调用的回调。

.Register() 函数中的最后一个参数是 ProperyMetadata 所在的位置。

附带说明一下,像 SolidColorBrushdp 这样的画笔类型会很好地自动分组到“属性”窗格的 Brush 组中,其他类型的 dp 会经常被列在 Common