gpt4 book ai didi

mvvm - Xamarin 表单自定义可绑定(bind)命令类型不匹配

转载 作者:行者123 更新时间:2023-12-03 10:14:07 26 4
gpt4 key购买 nike

我创建了一个名为 PINControl 的自定义 View ,它显示具有可配置位数的 PIN 条目。
我想在我的 ContentPage 中使用的 XAML是

<local:PINControl x:Name="PIN" 
PINLength="5"
PINCompleteCommand="{Binding CompletePIN}"
HorizontalOptions="CenterAndExpand" />

我在 PINControl 中的 BindableProperties 是:

public class PINControl : StackLayout
{
private const int LENGTH_DEFAULT = 4;

public static readonly BindableProperty PINLengthProp = BindableProperty.Create<PINControl, int> (c => c.PINLength, LENGTH_DEFAULT);
public static readonly BindableProperty PINCompleteCommandProp = BindableProperty.Create<PINControl, ICommand> (c => c.PINCompleteCommand, null);

public ICommand PINCompleteCommand {
get { return (ICommand)GetValue (PINCompleteCommandProp); }
set { SetValue (PINCompleteCommandProp, value); }
}
public int PINLength {
get { return (int)GetValue (PINLengthProp); }
set { SetValue (PINLengthProp, value); }
}

我的 ViewModel 包含

public ICommand CompletePIN { get; set; }

public PINViewModel ()
{
CompletePIN = new Command<string> ((pin) => {
var e = pin.ToString();
});
}
PINLength 似乎没有问题,但 PINCompleteCommand给我以下错误:

Cannot assign property "PINCompleteCommand": type mismatch between "Xamarin.Forms.Binding" and "System.Windows.Input.ICommand"



我找不到这个问题的解决方案。有人可以帮助我吗?

最佳答案

在命名 BindableProperties 时有一个很好的做法,即将其命名为 propertynameProperty .

在您的情况下,当 Xaml 解析器遇到此指令时

PINCompleteCommand="{Binding CompletePIN}" 

它首先尝试查找名为 PINCompleteCommandProperty 的公共(public)静态 BindableProperty,失败,然后查找名为 PINCompleteCommand 的普通属性,成功,并尝试将值(a Binding)分配给属性(an ICommand)并生成你看到的消息。

修复你的 BindableProperty 命名,你应该没问题。

关于mvvm - Xamarin 表单自定义可绑定(bind)命令类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com