MvvmCross Android EditText绑定不更新屏幕
本文关键字:更新 屏幕 绑定 Android EditText MvvmCross | 更新日期: 2023-09-27 18:09:08
我发现当使用MvvmCross版本3.5.1启用"不保留活动"开发人员设置时,MvvmCross EditText绑定无法正常工作。下面是复制的步骤:
- 创建一个新的核心&使用NuGet的"Getting Started"Mvvm交叉包的Droid项目。
- 从NuGet中添加ZXing.Net.Mobile PCL组件
-
实现ViewModel:
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>
-
在View中初始化ZXing.Net.Mobile库:
[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
. -
通过向
EditText
添加android:id
来编辑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数据绑定吗?
仅为texttedit添加绑定而不为EditText添加绑定。查看这里的实现:https://github.com/MvvmCross/MvvmCross/blob/4.0/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L85
您可以添加自定义绑定,如下所示:
- https://www.youtube.com/watch?feature=player_detailpage& v = taBoOenbpiU& t = 97年代
- http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
- MvvmCross UITextField自定义绑定