对于我一直在从事的一个项目,我正在将一些自定义控件从 WPF 平台移植到 UWP。 在 WPF 端,它是这样实现的:
public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.Register("MaxLength", typeof(int), typeof(HexBox), new PropertyMetadata(MaxLength_PropertyChanged));
public int MaxLength
{
get { return (int)GetValue(MaxLengthProperty); }
set { SetValue(MaxLengthProperty, value); }
}
private static void MaxLength_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HexBox hexControl = (HexBox)d;
hexControl.txtValue.MaxLength = (int)e.NewValue;
}
使用不带参数的MaxLength_PropertyChanged
。
当我尝试在 UWP 中执行相同操作时,我收到以下消息:
Argument 1: cannot convert from 'method group' to 'object'
我知道这与不传递参数或使用 () 将它们作为方法调用有关。但在 WPF 中,这种行为是隐式的。
有人有想法吗?
最佳答案
试试这个:
public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.Register(
"MaxLength",
typeof(int),
typeof(HexBox),
new PropertyMetadata(0, new PropertyChangedCallback(MaxLength_PropertyChanged))
);
private static void MaxLength_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HexBox hexControl = d as HexBox;
hexControl.txtValue.MaxLength = (int)e.NewValue;
}
关于c# - 无法从方法组转换为对象移植 WFP => UWP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44113491/