iam*_*hia 2 c# xamarin xamarin.forms
我有一个 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));
}
Run Code Online (Sandbox Code Playgroud)
而在我的XAML
:
<t:Button Text="{Binding ActBtnText}"
TapCommand="{Binding ActBtnCmd}"
WidthRequest="30"
Theme="{Binding Theme}" />
Run Code Online (Sandbox Code Playgroud)
在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'
Run Code Online (Sandbox Code Playgroud)
但是当我像下面这样更改我的属性时,消息就消失了:
public ICommand ActBtnCmd { get; set; }
public ICommand AdpBtnCmd { get; set; }
Run Code Online (Sandbox Code Playgroud)
谁能向我解释为什么我会收到这些消息?为什么我只能在 Android 中获得它?
这可能与绑定到的可绑定属性的绑定模式有关。
大多数属性的默认绑定模式是OneWay
. 当这些属性是数据绑定目标时,则从源设置目标属性。其中只需要一个get;
来自属性。
如果绑定模式如何,TwoWay
那么源属性将同时需要 aget;
和set;
. 这意味着当 a 属性是数据绑定目标时,目标是从源设置的(像往常一样),但源也是从目标设置的。
但是如果源属性是只读的(如 in get;
only),那么您可能会看到该消息,因为活页夹无法确保它可以写回源。
为了测试这个理论,我建议覆盖绑定模式
如果目标属性的默认绑定模式不适合特定数据绑定,则可以通过将 的
Mode
属性Binding
(或标记扩展的Mode
属性Binding
)设置为BindingMode
枚举成员之一来覆盖它。
<t:Button Text="{Binding ActBtnText}"
TapCommand="{Binding ActBtnCmd Mode=OneWay}" <!-- Note the Mode used -->
WidthRequest="30"
Theme="{Binding Theme}" />
Run Code Online (Sandbox Code Playgroud)
通过将它设置Mode
为OneWay
它应该让解析器知道不要期望set;
属性上的a并删除警告。
至于为什么平台上的运行时体验不同,可能是平台使用了不同的 XAML 解析器,其中使用不同的默认绑定模式或忽略该警告消息。
归档时间: |
|
查看次数: |
718 次 |
最近记录: |