在Android上使用Simple Mvvm Toolkit将ObservableCollection绑定到ListVi

本文关键字:ObservableCollection 绑定 ListVi Toolkit Mvvm Android Simple | 更新日期: 2023-09-27 18:26:17

我正试图使用以下代码将客户模型的集合绑定到ListView:

碎片内部

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            BindingSetup.Instance.Initlialize(this.Activity.ApplicationContext);
            // Create service agent and view model
            IProjectServiceAgent serviceAgent = new MockProjectServiceAgent();
            var viewModel = new ProjectViewModel(serviceAgent);
            // Create binding context, passing view model ... activity is an IMvxLayoutInflater interface
            _bindingContext = new MvxAndroidBindingContext(this.Activity, this.Activity as MainActivity, viewModel);
            // Create view by inflating binding on binding context 
            View view = _bindingContext.BindingInflate(Resource.Layout.central_projects, container, false);
            return view;
        }

central_projects.axml(片段视图):

   <?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">
      <ListView
          android:id="@+id/central_projects_expandableListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            local:MvxItemTemplate="@layout/test"
            local:MvxBind="ItemSource Customers" /> 
    </LinearLayout>

Test.axml(列表项):

 <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">
      <TextView
          android:id="@+id/epxlist_projectHeader_TextView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="DataHeader"
          android:layout_margin="2dp"
          local:MvxBind="Text CustomerName" />
    </LinearLayout>

ProjectViewModel.cs:

public class ProjectViewModel : ViewModelBase<ProjectViewModel>
            {
           private ObservableCollection<Customer> customers =
                  new ObservableCollection<Customer>();
                public ObservableCollection<Customer> Customers
                {
                    get { return customers; }
                    set
                    {
                        customers = value;
                        NotifyPropertyChanged(m => m.Customers);
                    }
                }
         public ProjectViewModel(IProjectServiceAgent serviceAgent)
                {
                    this.serviceAgent = serviceAgent;
                    this.Customers = Customer.CustomersList;
                }
         }

客户.cs(型号):

public class Customer : ModelBase<Customer>
    {
        // Manufacture a list of customers
        private static ObservableCollection<Customer> customersList;
        public static ObservableCollection<Customer> CustomersList
        {
            get
            {
                if (customersList == null)
                {
                    customersList = new ObservableCollection<Customer>
                    {
                        new Customer { CustomerName = "Bill", Orders = 1000 },
                        new Customer { CustomerName = "Steve", Orders = 2000 },
                        new Customer { CustomerName = "Mark", Orders = 3000 }
                    };
                }
                return customersList;
            }
        }
        private string customerName;
        public string CustomerName
        {
            get { return customerName; }
            set
            {
                customerName = value;
                NotifyPropertyChanged(m => m.CustomerName);
            }
        }

我只想在列表视图中显示客户的姓名。当我只使用一个简单的TextView时,绑定是有效的,但当我尝试绑定ListView时,它只显示一个空视图。有什么办法解决这个问题吗?

在Android上使用Simple Mvvm Toolkit将ObservableCollection绑定到ListVi

经过一周的努力找出问题所在,我找到了解决方案。

我不得不添加

viewCache.AddAssembly(typeof(MvxListView).Assembly);

BindingSetup.cs和片段视图中

<MvxListView
        android:id="@+id/central_projects_expandableListView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        local:MvxBind="ItemsSource Customers"
        local:MvxItemTemplate="@layout/test"/>

它工作得很好!