由于最近在等待新工作的入职,闲来无事还是完善一下我的WP8开发的博客吧。经过许久回头看一下我那半成品的WP应用,内牛满面,之前Google了N久的东西,居然也不记得了,真是不得不认老啊。

这段时间接触了一下B/S的东西,觉得那东西真不是人用的,一旦工程量上去,后续开发很大一部分时间都是在Debug中渡过,很是纠结。现在业内普遍的对移动开发的看法是——web应用是种趋势,可是个人觉得除非现阶段的移动网络环境有质的改变,否则用户体验会很有问题。然后简单的玩了一下node.js,觉得后台其实也可以不用那么复杂,想想,还是老老实实的写回自己的客户端应用好了。

废话就不多说了,先简单的介绍一下本篇的目的吧。MVC这个东西,现下看起来很流行,Model-View-Control,其实我个人并不清楚其概念所谓,其实说白了就是当年的“所见即所得”,一个意思。之前接触了一下WPF,就是给这个概念给搞死,但不管怎么样,xaml可能是MS的开发趋势,MCV又是其中一个极重要的概念,不入门不行。

MVC的更多的概念性解析请诸位百度或google吧,它的核心是数据,MVC是一种展现数据呈现方式的一种开发手段,虽然比纯代码性的呈现方式多了一些额外的工作,可是,必须要记住,现在做多一些是为了将来少做一些,其优势还是相当可观的。

(1)构造数据类DataItem

WinForm的项目里,我们想在程序设计的阶段预览ListBox是怎么显示数据,那几乎是不可能的,除非我将程序跑起来,在人类审美观崛起的现在,代码与界面分离设计可以有效的通过分工实现人力资源的最大化的利用,而这也是MVC的主要任务。

写WinForm时,我会这么设计一个简单的数据结构体:

struct TestDataItem{    int Index;    string Name;    boo Flag;}

可使用MVC的方式,编译器没办法识别这样的定义,所以必须将这简单的结构体扩展成一个特定的类:

//TestDataItem.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MvcTest{    ///     /// 数据集的Item类,继承INotifyPropertyChanged,INotifyPropertyChanging接口    ///     public partial class TestDataItem : INotifyPropertyChanged, INotifyPropertyChanging    {        ///         /// 属性更改的事件参数        ///         private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);        ///         /// 数据属性变化的事件回调        ///         public event PropertyChangingEventHandler PropertyChanging;        public event PropertyChangedEventHandler PropertyChanged;        ///         /// Item的成员属性        ///         private int _index;        private string _name;        private bool _flag;        /// 属性更改的响应函数        #region        partial void OnIndexChanging(int value);        partial void OnIndexChanged();        partial void OnNameChanging(string value);        partial void OnNameChanged();        partial void OnFlagChanging(bool value);        partial void OnFlagChanged();        #endregion                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /// 类接口的实现        #region        ///         /// INotifyPropertyChanging接口的实现函数        ///         protected virtual void SendPropertyChanging()        {            if ((this.PropertyChanging != null))            {                this.PropertyChanging(this, emptyChangingEventArgs);            }        }        ///         /// INotifyPropertyChanged接口的实现        ///         ///         protected virtual void SendPropertyChanged(String propertyName)        {            if ((this.PropertyChanged != null))            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        #endregion        /// 数据的公共属性        #region        public int Index        {            get            {                return this._index;            }            set            {                if(this._index != value)                {                    this.OnIndexChanging(value);                    this.SendPropertyChanging();                    this._index = value;                    this.SendPropertyChanged("Index");                    this.OnIndexChanged();                }            }        }        public string Name        {            get            {                return this._name;            }            set            {                if(this._name != value)                {                    this.OnNameChanging(value);                    this.SendPropertyChanging();                    this._name = value;                    this.SendPropertyChanged("Name");                    this.OnNameChanged();                }            }        }        public bool Flag        {            get            {                return this._flag;            }            set            {                if (this._flag != value)                {                    this.OnFlagChanging(value);                    this.SendPropertyChanging();                    this._flag = value;                    this.SendPropertyChanged("Flag");                    this.OnFlagChanged();                }            }        }        #endregion    }}

此类的核心是INotifyPropertyChanged,INotifyPropertyChanging这两个接口(不懂请百度呗,我也懂不了多少)。因为数据的窗口(像Grid或LongListSelector)要感知每一条TestDataItem的属性变化,然后这个类里面就存在各种的回调或事件啦。

(2)构造数据集TestDataItemCollection

这步理解就简单一点了,数据容器呈现的是整体的数据集合,这个集合也需要一个特殊的类,否则编译器不知道怎么去解析(我只能这么理解),有玩过数据绑定的童鞋应该能弄懂这点。

当然,TestDataItem的集合类也需要实现一个特殊的接口INotifyPropertyChanged——没错,跟上面TestDataItem实现的接口一样,可以这么理解,这个接口是MVC的核心所在。

//TestDataItemCollection.cs

using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MvcTest{    ///     /// 数据集的类    ///     public class TestDataCollection : INotifyPropertyChanged    {        ///         /// 数据集合        ///         public ObservableCollection
TestDataItems { get; private set; } ///
/// 属性更改事件回调 /// public event PropertyChangedEventHandler PropertyChanged; ///
/// 构造函数 /// public TestDataCollection() { this.TestDataItems = new ObservableCollection
(); } ///
/// INotifyPropertyChanged接口的实现 /// private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }}

(3)构造数据集示例ViewModel

这一步设计要呈现的数据例子,其用到的xmal的文件,可是在Visual Studio新建项的时候,并没有这一选项,可是我们会发现WP的页面文件格式就xaml的,所以不妨新建一个页面,将其重命名为TestDataViewModel.xaml,然后,删掉页面正面的cs等文件,将下面这段xaml代码粘上去——当然,你直接手敲上去也是没有问题的。

//TestDataViewModel.xaml

注意看xaml的注释内容,但看起来不难,难的一步是如何呈现上面的数据集于PhonePage上。

(4)呈现ViewModel数据集

首先,我们要选一个数据容器,个人比较喜欢LongListSelector,但似乎没有其分的选择,将上面的数据集例子绑定上去;然后,设计一下单条数据呈现的样式,要用到DataItemTemplate,这也许我将来会详讲。就这两条,直接上代码:

//MainPage.xaml

注意以上代码的两行注释跟第10行的内容,这是其关键所在,新建一个默认的WP工程,就多了总共三处地方,于是,你就能看到以下的效果啦:

于是,通过MVC的设计手段,我们就能看到在WP设计页面上看到数据集合是如何呈现的啦。至于实际运行如何加载数据集就不须要多说吧?只要给LongListSelector的ItemSource属性赋值就好了,一句话的功夫。

结语:

以上的是一个简单的MVC模式设计的例子,其实这工作一个人做看不出什么优势,可是,我们从中看出,这样的设计手法,可以让界面逻辑跟功能逻辑分开设计提高效率,还有,如果程序以后要改进,从预览窗口我们也更容易知道自己需要更改哪些东西,总之,优势不少就是啦。

自己还有一个问题,如何在同一个页面呈现两个数据集呢?嗯,这是个问题。对了,附上工程例子。