I have discovered a case where Mvvm Cross EditText binding fails to work correctly when the "Do not keep activities" developer setting is enabled using MvvmCross version 3.5.1. Here are the steps to reproduce:
Implement the 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;
});
}
}
}
Implement the View:
<?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>
Initialize the ZXing.Net.Mobile library in the View:
[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
.Edit the View XML by adding an android:id
to the EditText
.
<?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>
Rebuild and run the application. Now the scanned bar code is not displaying in the EditText
. The only change was giving the EditText
and android:id
. Does anyone understand why adding an android:id
would break MvvmCross data binding?
The binding is only added for TextEdit and not for EditText. See the implementation here: https://github.com/MvvmCross/MvvmCross/blob/4.0/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L85
You could add a custom binding as explained in:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With