wpfmvvm,基于MVVM下的Silverlight之增删改

上一篇文章写得查询比较简单,这次做一个基于MVVM下的增删改。只要按照步骤来,没有不会的。
第一步:创建一个silverlight项目;
第二步:添加项目对 GalaSoft.MvvmLight.Extras.SL4,GalaSoft.MvvmLight.SL4, System.Windows.Controls.Data, Microsoft.Practices.Unity.Silverlight的引用
第三步:创建相应的文件夹ViewModel和Model 和View 。
第四步:在Model文件夹下新建一个类Student.cs类,代码如下:
public class Student
{
public string Name { get; set; }//名字
public int Age { get; set; } //年龄
public int SNo { get; set; }//学号
}
第五步:还是在Model文件夹下新建一个类Students.cs类 ,这个类的作用是用于构造数据。
private ObservableCollection _getstudents;
public ObservableCollection GetStudent()
{
_getstudents = new ObservableCollection(){
new Student{ Name="张三", Age=21, SNo=11},
new Student{ Name="李四", Age=22, SNo=12},
new Student{ Name="王五", Age=23, SNo=13},
new Student{ Name="高尚", Age=24, SNo=14},
};
return _getstudents;
}
第六步:在ViewModel下创建StudentViewModel.cs类,这个类主要是连接model和view的
第七步:从这开始就是怎么把ViewModel里的数据输送到前台的。
首先在ViewModel里创建ViewModelLocator.cs,我们叫它ViewModel加载器
里面的代码如下:
using GalaSoft.MvvmLight; using Microsoft.Practices.Unity; //需要引入Microsoft.Practices.Unity.Silverlight 命名空间
public class ViewModelLocator {
public static IUnityContainer Container { get; private set; }
//创建一个容器,把StudentViewModel 扔到这个容器里,前台只需要在这个容器里取值就行了。 static ViewModelLocator() { Container = new UnityContainer();
Container.RegisterType(new ContainerControlledLifetimeManager()); } /// /// 首页操作 /// public StudentViewModel MyStu { get { return Container.Resolve(); } }
}
第二步:在App.xaml 里添加代码,引用ViewModelLocator.cs里的东东。


xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
这个就是viewmodel那个文件夹
xmlns:vm="clr-namespace:SilverlightApplication1.ViewModel"
mc:Ignorable="d">
别名是Locator




第八步:前台处理
这样一个简单的查询就做完了。注意这里,MainPage.xaml没有写一句代码

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
引入的一些东西,有的具体是干嘛的,我也不太懂。
需要引入System.Windows.Controls.Data命名空间 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
首先为页面设置DataContext为StudentViewModel,
注意观察这个 MyStu对应的是ViewModelLocator.cs里的类名 Source={StaticResource Locator} 还记得那个别名吗?呵呵,就是这个
DataContext="{Binding MyStu, Mode=TwoWay, Source={StaticResource Locator}}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" >
全局调用MyStu对应类下面的RefreshCommand。就是StudentViewModel下的RefreshCommand ,进行查询







ItemsSource="{Binding Students, Mode=TwoWay}" 绑定数据源Students







调用StudentViewModel下的DelCommand方法,实现删除。












第二讲:添加我们直接从第六步开始写,首先StudentViewModel
下写一个添加的方法。 AddCommand = new RelayCommand(() =>
{
Student stu = new Student();
stu.Name = stuName;
stu.Age = stuAge;
stu.SNo = randmom.Next(1, 100);
Students.Add(stu);
});
这样,前台添加按钮绑定上AddCommand 就可以了。
前台


做完上面练习,基本上也就会做了。
怎么分享源代码呢?不会呀。
Tags:  mvvm命令绑定 mvvm树形 mvvm模式 wpfmvvm

延伸阅读

最新评论

发表评论