ListViewPage : ContentPage { ContextViewModel viewModel; public ListViewPage() { InitializeComponent(); this.BindingContext = viewModel = new ContextViewModel(); } private void MenuItem_Clicked(object sender, EventArgs e) { var mi = ((MenuItem)sender); ContextModel date = mi.CommandParameter as ContextModel; Navigation.PushAsync(new ContextModelPage()); MessagingCenter.Send<ListViewPage,ContextModel>(, date); } MenuItem_Clicked_1(object sender, EventArgs e) { var mi = ((MenuItem)sender); int id = Convert.ToInt32( mi.CommandParameter); var date = await viewModel.DeleteItem(id); if (!date) { , , ); } } private void ToolbarItem_Clicked(object sender, EventArgs e) { Navigation.PushAsync(new ContextModelPage()); } }
这里,我们绑定了ContextViewModel,然后编写了界面上的各种交互事件.
以上,我们的列表也就算完成了,下面我们来看看我们的增加和修改页面.(也就是显示详细数据的页面)
如下:
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class
这里我们采用了前面系列中讲过的Label ,Entry,DatePicker ,Editor ,编写后台代码如下:
public partial class ContextModelPage : ContentPage { private int isUpdate = 0; public ContextModelPage() { InitializeComponent(); MessagingCenter.Subscribe(this, "GetModel", (obj, item) => { //DisplayAlert("提示", "传过来的参数为" + item, "确定"); this.times.Date = item.AddTime.Value; this.titel.Text = item.Title; this.contexts.Text = item.Context; isUpdate = item.ID; }); } private void BtnSave_Clicked(object sender, EventArgs e) { if (isUpdate>0) { ContextModel model = new ContextModel(); model.AddTime = times.Date; model.Context = contexts.Text; model.Title = titel.Text; model.ID = isUpdate; MessagingCenter.Send(this, "UpdateItem", model); } else { ContextModel model = new ContextModel(); model.AddTime = times.Date; model.Context = contexts.Text; model.Title = titel.Text; MessagingCenter.Send(this, "AddItem", model); } } protected override void OnDisappearing() { MessagingCenter.Unsubscribe(this, "GetModel"); base.OnDisappearing(); } }
这里,我们编写页面的点击等交互事件,然后我们采用通讯中心(MessagingCenter)的方式来传递修改和删除的信息给后台的ViewModel.
至此,就完成了整个的简易增删改查的编写.
写在最后
本系列到此,就已经进行了一大半了..后面会继续更新一些安卓库的绑定等内容,敬请期待.