为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 UWP 中的另一个值更改 DependencyProperty 值

如何从 UWP 中的另一个值更改 DependencyProperty 值

C#
眼眸繁星 2022-07-10 10:38:33
我正在 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 中获得相同的结果?
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

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

.Register()函数中的最后一个参数是去哪里ProperyMetadata

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


查看完整回答
反对 回复 2022-07-10
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号