在Xamarin.Forms中使用MVVM绑定是一种有用的方法来实现数据绑定,但对于初学者来说可能会有些困难。下面是一个简单的示例,展示了如何使用MVVM绑定将数据显示在列表视图中。
using System.Collections.ObjectModel;
using System.ComponentModel;
using MyApp.Models;
namespace MyApp.ViewModels
{
public class MyListViewModel : INotifyPropertyChanged
{
public ObservableCollection MyItems { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public MyListViewModel()
{
MyItems = new ObservableCollection()
{
new MyItem() { Title = "Item 1", Description = "This is Item 1." },
new MyItem() { Title = "Item 2", Description = "This is Item 2." },
new MyItem() { Title = "Item 3", Description = "This is Item 3." },
};
}
}
}
using System;
using System.Collections.Generic