温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WP8开发日志(2):MVC设计模式入门

发布时间:2020-08-04 09:42:44 来源:网络 阅读:861 作者:shengqin105 栏目:开发技术

由于最近在等待新工作的入职,闲来无事还是完善一下我的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
{
    /// <summary>
    /// 数据集的Item类,继承INotifyPropertyChanged,INotifyPropertyChanging接口
    /// </summary>
    public partial class TestDataItem : INotifyPropertyChanged, INotifyPropertyChanging
    {
        /// <summary>
        /// 属性更改的事件参数
        /// </summary>
        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
        /// <summary>
        /// 数据属性变化的事件回调
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Item的成员属性
        /// </summary>
        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
        /// <summary>
        /// INotifyPropertyChanging接口的实现函数
        /// </summary>
        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }
        /// <summary>
        /// INotifyPropertyChanged接口的实现
        /// </summary>
        /// <param name="propertyName"></param>
        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
{
    /// <summary>
    /// 数据集的类
    /// </summary>
    public class TestDataCollection : INotifyPropertyChanged
    {
        /// <summary>
        /// 数据集合
        /// </summary>
        public ObservableCollection<TestDataItem> TestDataItems { get; private set; }
        /// <summary>
        /// 属性更改事件回调
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 构造函数
        /// </summary>
        public TestDataCollection()
        {
            this.TestDataItems = new ObservableCollection<TestDataItem>();
        }
        /// <summary>
        /// INotifyPropertyChanged接口的实现
        /// </summary>
        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

<!-- 这对应的是数据集的类名,表示一组数据-->
<vm:TestDataCollection
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MvcTest">
    <!--clr-namespace:MvcTest这是数据集的命名空间,带文件夹的要注意啦-->
    <!--简单的数据集例子-->
    <vm:TestDataCollection.TestDataItems>
        <vm:TestDataItem Index="0" Name="name1" Flag="False"></vm:TestDataItem>
        <vm:TestDataItem Index="1" Name="name2" Flag="True"></vm:TestDataItem>
        <vm:TestDataItem Index="2" Name="name3" Flag="True"></vm:TestDataItem>
        <vm:TestDataItem Index="3" Name="name4" Flag="False"></vm:TestDataItem>
        <vm:TestDataItem Index="4" Name="name5" Flag="True"></vm:TestDataItem>
        <vm:TestDataItem Index="5" Name="name6" Flag="True"></vm:TestDataItem>
    </vm:TestDataCollection.TestDataItems>
</vm:TestDataCollection>


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


(4)呈现ViewModel数据集


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

//MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="MvcTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DataContext="{d:DesignData TestDataViewModel.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
                                                                                                                                                            
    <!--单条数据显示的样式-->
    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="TestDataItemTemplate">
            <StackPanel Margin="0,0,0,0">
                <TextBlock Text="{Binding Index}"/>
                <TextBlock Text="{Binding Name}" />
                <!--TextBlock Text="{Binding Flag}" /-->
                <StackPanel Height="1" Background="AliceBlue"></StackPanel>
            </StackPanel>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <!--用一个LongListSelector来显示数据集-->
            <StackPanel>
                <phone:LongListSelector Margin="0,0,0,0" ItemTemplate="{StaticResource ResourceKey=TestDataItemTemplate}" ItemsSource="{Binding TestDataItems}"/>
            </StackPanel>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>


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

WP8开发日志(2):MVC设计模式入门


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


结语:


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


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



附件:http://down.51cto.com/data/2364279
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI