silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了MVVM(Model-View-ViewModel)实例讲解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

MVVM(Model-View-ViewModel)实例讲解 2009年12月31日 分类: Silverlight, WPF MVVM模式大家应该不陌生吧,陌生的快来看看,可是WPF/Silverlight开发中,必备的设计模式。 MVVM模式解决了,我们在开发WPF/Silverlight应用程序过程中产生的业务层、表示层比较混乱问题,使表示层和业务层完全分离。 早在2005年,John Go
@H_308_3@mVVM(Model-View-viewmodel)实例讲解
2009年12月31日 分类: Silverlight,WPF
@H_288_30@mVVM模式大家应该不陌生吧,陌生的快来看看,可是WPF/Silverlight开发中,必备的设计模式。@H_299_31@mVVM模式解决了,我们在开发WPF/Silverlight应用程序过程中产生的业务层、表示层比较混乱问题,使表示层和业务层完全分离。
早在2005年,John Gossman写了一篇关于Model-View-viewmodel模式的博文,这种模式被他所在的微软的项目组用来创建Expression Blend。

MVVM(Model-View-ViewModel)实例讲解

从上图可以看出来,View表示层就是我们通常的XAML,用来表示前台界面,viewmodel视图模块层的作用用来连接业务逻辑和视图层的关键部分,通常我们发出的命令或者事件都是通过这层传送给业务逻辑层的,Model就是我们的实际数据,业务逻辑代码等。

下面我们用一个Silverlight简单例子来讲解MVVM模式

MVVM(Model-View-ViewModel)实例讲解

这个程序就是实现简单查询,输入ID号,查询符合结果的内容

新建一个Silverlight项目,并按照下图新建目录

MVVM(Model-View-ViewModel)实例讲解

Command我们用来存放查询用的命令,Model我们用来存放数据,View我们用来存放@L_616_18@查询的UserControl,viewmodel我们用来存放查询viewmodel

我们先建立Model层用来存储访问要查询的数据

1
2
3
4
5
6
public class DataItem
{
     public int ID { get ; set ; }
     public String Name { get ; set ; }
}
1
2
3
4
5
6
7
8
9
10
@H_654_197@ 11
12
13
14
15
16
17
@H_489_211@ 18
19
20
@H_403_205@ 21
22
23
24
25
26
27
28
29
30
public static class DataDemo
{
     private static Collection<DataItem> _DataList = null ;
  
     public static Collection<DataItem> DataList
     {
             if (_DataList == null )
             {
@H_654_197@                  _DataList = InitDataList();
             }
             return _DataList;
     }
  
     private static Collection<DataItem> InitDataList()
@H_489_211@      {
         Collection<DataItem> lists = new Collection<DataItem>();
         for ( int i = 0; i < 100; i++)
@H_403_205@          {
             DataItem item = new DataItem();
             item.ID = i + 1;
             item.Name = "例子" + (i + 1);
             lists.Add(item);
         return lists;
     }
}

接下来,我们新建UserControl用来表示查询页面(View)

1
2
3
4
5
6
7
8
9
10
@H_654_197@ 11
12
< UserControl x:Class = "MVVMDemo.View.QueryData"
  xmlns:local = "clr-namespace:MVVMDemo.viewmodel"
  @H_528_582@mc:Ignorable = "d" Height = "256" Width = "256" >
     < Grid x:Name = "LayoutRoot" >
         < Button x:Name = "btnSearch" Height = "24" HorizontalAlignment = "Left" @H_528_582@margin = "164,8,0" VerticalAlignment = "Top" Width = "84" Content = "搜索" />
         < TextBox x:Name = "txtKeyword" Height = "24" HorizontalAlignment = "Left" @H_528_582@margin = "8,0" VerticalAlignment = "Top" Width = "152" textwrapping = "Wrap" d:LayoutOverrides = "HorizontalAlignment" />
         < TextBox x:Name = "txtResult" HorizontalAlignment = "Left" @H_528_582@margin = "8,36,8" Width = "240" textwrapping = "Wrap" d:LayoutOverrides = "VerticalAlignment" />
@H_654_197@      </ Grid >
</ UserControl >

到这里我们已经建好了Model,View层,接下来,我们建立viewmodel

1
2
3
4
5
6
7
8
9
10
@H_654_197@ 11
12
13
14
15
16
17
@H_489_211@ 18
19
20
@H_403_205@ 21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@H_419_1165@ 79
80
81
82
83
84
85
86
87
88
@H_222_1197@ 89
90
91
92
93
94
95
96
97
public class QueryDataviewmodel : INotifyPropertyChanged
{
     #region 变量
     /// <sumMary>
     /// 查询的数据
     /// </sumMary>
     private Collection<DataItem> _DataList = null ;
     /// <sumMary>
     /// 查询命令
     /// </sumMary>
@H_654_197@      private ICommand _QueryCommand = null ;
     /// <sumMary>
     /// 搜索关键字
     /// </sumMary>
     private String _SearchText = String .Empty;
     /// <sumMary>
     /// 搜索结果
@H_489_211@      /// </sumMary>
     private String _SearchResult = String .Empty;
     #endregion
@H_403_205@   
     #region 属性
     /// <sumMary>
     /// 搜索关键字
     /// </sumMary>
     public String SearchText
     {
         get { return this ._SearchText; }
         set
         {
             this ._SearchText = value;
             if ( this .PropertyChanged != null )
                 this .PropertyChanged( this , new PropertyChangedEventArgs( "SearchText" ));
         }
     }
     /// <sumMary>
     /// 搜索结果
     /// </sumMary>
     public String SearchResult
     {
         get { return this ._SearchResult; }
         set
         {
             this ._SearchResult = value;
  
             if ( this .PropertyChanged != null )
                 this .PropertyChanged( this , new PropertyChangedEventArgs( "SearchResult" ));
         }
     }
     /// <sumMary>
     /// 查询命令
     /// </sumMary>
     public ICommand QueryCommand
     {
         get { return _QueryCommand; }
     }
     #endregion
  
     #region 构造函数
     public QueryDataviewmodel(Collection<DataItem> dataList)
     {
         this ._DataList = dataList;
         _QueryCommand = new QueryDataCommand( this );
     }
     #endregion
  
     #region 方法
     /// <sumMary>
     /// 查询数据
     /// </sumMary>
     public void QueryData()
     {
         if (! String .IsNullOrEmpty( this .SearchText))
         {
             DataItem dataItem = null ;
             foreach (DataItem item in this ._DataList)
             {
                 if (item.ID.ToString() == this .SearchText)
@H_419_1165@                  {
                     dataItem = item;
                     break ;
                 }
             }
             if (dataItem != null )
             {
                 this .SearchResult = String .Format( "ID:{0}/nName:{1}" ,dataItem.ID,dataItem.Name);
             }
         }
@H_222_1197@      }
     #endregion
  
     #region INotifyPropertyChanged 成员
  
     public event PropertyChangedEventHandler PropertyChanged;
  
     #endregion
}

大佬总结

以上是大佬教程为你收集整理的MVVM(Model-View-ViewModel)实例讲解全部内容,希望文章能够帮你解决MVVM(Model-View-ViewModel)实例讲解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:modelmvvmviewviewmodel实例讲解
猜你在找的silverlight相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap