MvvmCross Android EditText绑定未更新屏幕

5

我发现在使用MvvmCross 3.5.1版本时,当启用“不保留活动”开发人员设置时,Mvvm Cross EditText绑定无法正常工作。以下是复现步骤:

  1. Create a new Core & Droid project using the "Getting Started" Mvvm Cross packages from NuGet.
  2. Add the ZXing.Net.Mobile PCL component from NuGet.
  3. 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;
                    });
                }
            }
        }
    
  4. 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>
    
  5. 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);
        }
    }
    
  6. Run the application and scan a bar code. You can use this Barcodesinc bar code generator and scan from your monitor if you don't have any bar codes handy. The scanned bar code should appear in the EditText.
  7. 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>
    
  8. 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?


1
如果值不是空字符串,那么只有在设置了ScannedBarCode时会发生什么? - Giorgi
@Giorgi 这并没有改变任何事情。EditText 在屏幕上仍然是空的。ScannedBarCode 属性仍然保持我扫描的值。就像 ViewModel + View 不同步一样。我已经在上面添加了一个编辑,包含了自我提问以来我学到的更多信息。 - Trevor Balcom
1个回答

2

5
EditText 继承自 TextView,因此适用于两者,绑定 Text 属性对 EditText 也有效。这在我的多个应用程序中都可以使用,并且在 MvvmCross-Tutorials 的许多示例中也可以使用。因此问题不在这里。 - Cheesebaron
如果我只是注释掉条形码扫描部分,并输入一些愚蠢的东西,比如ScannedBarCode =“Hello, World”,那么它会按预期更新屏幕上的EditText。 - Trevor Balcom
3
我已经知道如何重现这个问题了。如果EditText有一个android:id,那么数据绑定会失败。如果EditText没有android:id,那么数据绑定就像预期的那样正常工作。你知道这是什么意思吗?也就是说,我在这里做错了什么,还是MvvmCross绑定框架可能存在bug? - Trevor Balcom
我在我的项目中遇到了完全相同的问题,只有在进行静态分析时才会出现。感谢您指出如何解决它(删除id)。@Martijn00 有什么想法吗? - Jonathan ANTOINE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接