gpt4 book ai didi

c# - 属性与 get 的绑定(bind)问题;与获取相比;放;在 Xamarin.Android 中

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:09 26 4
gpt4 key购买 nike

我有一个 XF 应用程序。在我的 ViewModel 中,我有以下内容:

public ICommand ActBtnCmd { get; }
public ICommand AdpBtnCmd { get; }

public SettingsTabViewModel(SettingsTabPage settingsTabPage)
{
ActBtnCmd = new Command<Templates.Button>((btn) => MessagingCenter.Send(this, "ActBtn", btn));
AdpBtnCmd = new Command<Templates.Button>((btn) => MessagingCenter.Send(this, "AdpBtn", btn));
}

在我的 XAML 中:

<t:Button Text="{Binding ActBtnText}" 
TapCommand="{Binding ActBtnCmd}"
WidthRequest="30"
Theme="{Binding Theme}" />

在iOS中调试没问题。但是,当我在 Android 中调试应用程序时,我会在“应用程序输出”窗口中收到以下消息:

Binding: 'ActBtnCmd' property not found on 'xxx.SettingsTabViewModel', target property: 'xxx.Templates.Button.TapCommand'
Binding: 'AdpBtnCmd' property not found on 'xxx.SettingsTabViewModel', target property: 'xxx.Templates.Button.TapCommand'

但是当我像下面这样更改我的属性时,消息就消失了:

public ICommand ActBtnCmd { get; set; }
public ICommand AdpBtnCmd { get; set; }

任何人都可以向我解释为什么我会收到这些消息吗?为什么我只能在 Android 中获得它?

最佳答案

这可能与要绑定(bind)到的可绑定(bind)属性的绑定(bind)模式有关。

大多数属性的默认绑定(bind)模式是OneWay。当这些属性是数据绑定(bind)目标时,目标属性是从源设置的。这只需要 get; 来自属性。

如果绑定(bind)模式是TwoWay,那么源属性将同时需要get;set;。这意味着当 a 属性是数据绑定(bind)目标时,目标是从源设置的(像往常一样),但源也是从目标设置的。

但如果源属性是只读的(如 get; only),那么您可能会看到该消息,因为绑定(bind)器无法确保它可以写回源。

为了验证这个理论,我建议 Overriding the Binding Mode

If the default binding mode on the target property is not suitable for a particular data binding, it's possible to override it by setting the Mode property of Binding (or the Mode property of the Binding markup extension) to one of the members of the BindingMode enumeration.

<t:Button Text="{Binding ActBtnText}" 
TapCommand="{Binding ActBtnCmd Mode=OneWay}" <!-- Note the Mode used -->
WidthRequest="30"
Theme="{Binding Theme}" />

通过将 Mode 设置为 OneWay,它应该让解析器知道不要期望属性上的 set; 并删除警告。

至于为什么平台上的运行时体验不同,可能是平台使用不同的 XAML 解析器,其中使用不同的默认绑定(bind)模式或忽略该警告消息。

引用 Xamarin.Forms Binding Mode

关于c# - 属性与 get 的绑定(bind)问题;与获取相比;放;在 Xamarin.Android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204483/

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