【问题标题】:MvvmCross Android EditText binding not updating screenMvvmCross Android EditText 绑定不更新屏幕
【发布时间】:2015-08-20 15:58:07
【问题描述】:

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

  1. 使用来自 NuGet 的“入门”Mvvm Cross 包创建一个新的 Core & Droid 项目。
  2. 从 NuGet 添加 ZXing.Net.Mobile PCL 组件。
  3. 实现 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. 实现视图:

    <?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. 在视图中初始化 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);
        }
    }
    
  6. 运行应用程序并扫描条形码。如果您手边没有任何条形码,您可以使用此Barcodesinc bar code generator 并从您的显示器扫描。扫描的条形码应出现在EditText
  7. 通过将android:id 添加到EditText 来编辑视图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>
    
  8. 重建并运行应用程序。 现在EditText 中不显示扫描的条形码。唯一的变化是提供EditTextandroid:id。有谁明白为什么添加android:id 会破坏 MvvmCross 数据绑定?

【问题讨论】:

  • 如果仅在值不是空字符串时设置 ScannedBarCode 怎么办?
  • @Giorgi 这不会改变任何事情。屏幕上的 EditText 仍然是空的。 ScannedBarCode 属性仍然保存我扫描的值。就像 ViewModel + View 不同步一样。我在上面添加了一个 EDIT,它添加了自从我最初提出这个问题以来我学到的更多信息。

标签: c# xamarin xamarin.android mvvmcross zxing


【解决方案1】:

【讨论】:

  • EditText 继承自 TextView,因此它适用于两者,并且绑定 Text 属性确实适用于 EditText。它适用于我的多个应用程序和 MvvmCross-Tutorials 中的示例负载。所以问题不存在。
  • 如果我只是注释掉条码扫描部分,并放入像 ScannedBarCode = "Hello, World" 这样愚蠢的东西,那么它会按预期更新屏幕上的 EditText。
  • @Cheesebaron 我想出了如何重现这个问题。如果 EditText 有一个 android:id 则绑定失败。如果 EditText 没有 android:id 则绑定按预期工作。你知道那是什么意思吗?即,我在这里做的是不正确的事情,还是这可能是 MvvmCross 绑定框架中的错误?
  • 我这边也有同样的问题,只有在静态分析完成后才处于发布模式。感谢您指出如何解决它(删除 id)。 @Martijn00 有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2015-07-12
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
相关资源
最近更新 更多