C# 在Android上使用简单Mvvm工具包将ObservableCollection绑定到ListView
我正在尝试使用以下代码将客户模型集合绑定到ListView: 片段内部:C# 在Android上使用简单Mvvm工具包将ObservableCollection绑定到ListView,c#,android,listview,mvvm,xamarin,C#,Android,Listview,Mvvm,Xamarin,我正在尝试使用以下代码将客户模型集合绑定到ListView: 片段内部: public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { BindingSetup.Instance.Initlialize(this.Activity.ApplicationContext); //
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\u 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>
<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>
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;
}
}
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);
}
}
Test.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>
<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>
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;
}
}
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);
}
}
ProjectViewModel.cs:
<?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>
<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>
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;
}
}
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);
}
}
公共类ProjectViewModel:ViewModelBase
{
私人可观测收集客户=
新的可观察集合();
公开收集客户
{
获取{返回客户;}
设置
{
顾客=价值;
NotifyPropertyChanged(m=>m.Customers);
}
}
公共项目视图模型(IProjectServiceAgent服务代理)
{
this.serviceAgent=serviceAgent;
this.Customers=Customer.CustomersList;
}
}
Customer.cs(型号):
<?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>
<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>
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;
}
}
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);
}
}
公共类客户:模型库
{
//制作客户名单
私有静态ObservableCollection客户列表;
公共静态ObservableCollection客户列表
{
收到
{
如果(CustomerList==null)
{
CustomerList=新的ObservableCollection
{
新客户{CustomerName=“Bill”,订单=1000},
新客户{CustomerName=“Steve”,订单=2000},
新客户{CustomerName=“Mark”,订单=3000}
};
}
返回客户列表;
}
}
私有字符串客户名称;
公共字符串客户名称
{
获取{return customerName;}
设置
{
客户名称=价值;
NotifyPropertyChanged(m=>m.CustomerName);
}
}
我只想在listview中显示客户的姓名。当我只使用一个简单的TextView时,绑定工作正常,但当我尝试绑定listview时,它只显示一个空视图。有什么办法可以解决这个问题吗?经过一周的努力,我找到了解决方案 我必须补充一点
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"/>
它工作得很好