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

时间:2014-10-13 09:31:05

标签: c# android listview mvvm xamarin

我正在尝试使用以下代码将Customer Model的集合绑定到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;
                }
         }

Customer.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);
            }
        }

我只想在listview中显示客户的名字。当我只使用一个简单的TextView时,绑定工作正常但当我尝试绑定ListView时只显示一个空视图。有什么想法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在试图找出问题所在的一周之后,我找到了解决方案。

我必须添加

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"/>

它完美地运作!