【发布时间】:2014-10-13 09:31:05
【问题描述】:
我正在尝试使用以下代码将客户模型的集合绑定到 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);
}
}
我只想在列表视图中显示客户的姓名。当我只使用一个简单的 TextView 时,绑定正在工作,但是当我尝试绑定 ListView 时,它只显示一个空视图。有什么想法可以解决这个问题吗?
【问题讨论】:
标签: c# android listview mvvm xamarin