我发现一种情况,当使用MvvmCross版本3.5.1启用“不保留事件”开发人员设置时,Mvvm跨EditText绑定(bind)无法正常工作。以下是重现步骤:
public class FirstViewModel : MvxViewModel
{
private readonly IMobileBarcodeScanner _mobileBarcodeScanner;
public FirstViewModel(IMobileBarcodeScanner mobileBarcodeScanner)
{
_mobileBarcodeScanner = mobileBarcodeScanner;
}
private string _barCode = "";
public string BarCode
{
get { return _barCode; }
set { _barCode = value; RaisePropertyChanged(() => BarCode); }
}
private MvxCommand _scanBarCodeCommand;
public IMvxCommand ScanBarCodeCommand
{
get
{
return _scanBarCodeCommand ?? (_scanBarCodeCommand = new MvxCommand(async () => await OnScanBarCode()));
}
}
private async Task OnScanBarCode()
{
var result = await _mobileBarcodeScanner.Scan();
if (result != null && !string.IsNullOrEmpty(result.Text))
{
InvokeOnMainThread(() =>
{
BarCode = result.Text;
});
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text BarCode" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"
local:MvxBind="Click ScanBarCodeCommand" />
</LinearLayout>
[Activity(Label = "View for FirstViewModel")]
public class FirstView : MvxActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.FirstView);
MobileBarcodeScanner.Initialize(Application);
}
}
EditText
中。 android:id
添加到EditText
来编辑View XML。<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/scan_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text BarCode" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"
local:MvxBind="Click ScanBarCodeCommand" />
</LinearLayout>
EditText
中显示。唯一的变化是提供了EditText
和android:id
。有谁知道为什么添加android:id
会破坏MvvmCross数据绑定(bind)? 最佳答案
绑定(bind)仅为TextEdit添加,而不为EditText添加。在此处查看实现:https://github.com/MvvmCross/MvvmCross/blob/4.0/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L85
您可以按照以下说明添加自定义绑定(bind):
关于c# - MvvmCross Android EditText绑定(bind)未更新屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32122966/