Silverlight之MVVM:模型-视图-视图模型(Model-View-ViewModel)(16)

Silverlight之MVVM:模型-视图-视图模型(Model-View-ViewModel)(16),第1张

概述Silverlight之MVVM:模型视图-视图模型(Model-View-ViewModel)(16)   概述 组成部分Model、View、ViewModel,程序=数据结构+算法。Model就是数据结构,ViewModel实现算法数据处理,View实现数据展现。 View:UI界面 ViewModel:它是View的抽象,负责View与Model之间信息转换,将View的Command传

Silverlight之MVVM:模型-视图-视图模型(Model-VIEw-viewmodel)(16)

 


概述
组成部分Model、VIEw、viewmodel,程序=数据结构+算法。Model就是数据结构,viewmodel实现算法数据处理,VIEw实现数据展现。
VIEw:UI界面
viewmodel:它是VIEw的抽象,负责VIEw与Model之间信息转换,将VIEw的Command传送到Model;
Model:数据层

VIEw与VIEwModule连接可以通过下面的方式

Binding Data:实现数据的传递
Command:实现 *** 作的调用
AttachBehavior:实现控件加载过程中的 *** 作

VIEw没有大量代码逻辑。结合WPF、Silverlight绑定机制,MVP演变出了MVVM,充分利用了WPF、Silverlight的优势,将大量代码逻辑、状态转到viewmodel,
可以说MVVM是专门为WPF、Silverlight打造的。

VIEw绑定到viewmodel,然后执行一些命令在向它请求一个动作。而反过来,viewmodel跟Model通讯,告诉它更新来响应UI。
这样便使得为应用构建UI非常的容易。往一个应用程序上贴一个界面越容易,外观设计师就越容易使用Blend来创建一个漂亮的界面。
同时,当UI和功能越来越松耦合的时候,功能的可测试性就越来越强。

下面是一个具体的代码实现例子:

1.Model

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;

namespace MVVM模式.Model
{
    public class AddModel : DependencyObject
    {
        public static Readonly DependencyProperty Num1Property = DependencyProperty.Register("Num1",typeof(int),typeof(AddModel),null);
        public int Num1
        {
            get { return (int)GetValue(Num1Property); }
            set { SetValue(Num1Property,value); }
        }

 

        public int Num2
        {
            get { return (int)GetValue(Num2Property); }
            set { SetValue(Num2Property,value); }
        }

        // Using a DependencyProperty as the backing store for Num2.  This enables animation,styling,binding,etc...
        public static Readonly DependencyProperty Num2Property =
            DependencyProperty.Register("Num2",null);

        public static Readonly DependencyProperty ResoultProperty = DependencyProperty.Register("Resoult",null);
        public int Resoult
        {
            get { return (int)GetValue(ResoultProperty); }
            set { SetValue(ResoultProperty,value); }
        }

    

    }
}


2.viewmodel

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using MVVM模式.Model;
namespace MVVM模式.viewmodel
{
    public class Addviewmodel
    {

        public AddModel MyModel
        {
            get { return new AddModel(); }
        }

        public ICommand AddCommand
        {
            get
            {
                return new AddParams();
            }
        }
    }

    public class AddParams : ICommand
    {
        public bool CanExecute(object parameter)
        {
           return true;
        }

        public event EventHandler CanExecuteChanged;

        public voID Execute(object parameter)
        {
          AddModel  am=parameter as AddModel;
          am.Resoult = am.Num1 + am.Num2;
        }
    }
}

3.VIEw
后台代码:
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using MVVM模式.viewmodel;
namespace MVVM模式
{
    public partial class MainPage : UserControl
    {
        private Addviewmodel am = new Addviewmodel();
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = am.MyModel;
            this.button1.Command = am.AddCommand;
        }

     

    }
}
前台代码:

<UserControl x:Class="MVVM模式.MainPage"
    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="@L_419_3@"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWIDth="400">

    <GrID x:name="LayoutRoot" Background="White">
        <TextBox Height="23" HorizontalAlignment="left" margin="42,70,0" name="textBox1" VerticalAlignment="top" WIDth="120" Text="{Binding Num1,Mode=TwoWay}" />
        <TextBox Height="23" HorizontalAlignment="left" margin="213,0" name="textBox2" VerticalAlignment="top" WIDth="120" Text="{Binding Num2,Mode=TwoWay}" />
 
        <TextBox Height="23" HorizontalAlignment="left" margin="127,120,0" name="textBox3" VerticalAlignment="top" WIDth="120" Text="{Binding Resoult,Mode=TwoWay}"/>
        <TextBlock Height="23" HorizontalAlignment="left" margin="178,71,0" name="textBlock1" Text="+" VerticalAlignment="top" />
        <button Content="计算" Height="23" HorizontalAlignment="left" margin="149,150,0" CommandParameter="{Binding}" name="button1" VerticalAlignment="top" WIDth="75" />
    </GrID>
</UserControl>

4.注意附加属性应用

  public int Num1
        {
            get { return (int)GetValue(Num1Property); }
            set { SetValue(Num1Property,value); }
        }

        // Using a DependencyProperty as the backing store for Num1.  This enables animation,etc...
        public static Readonly DependencyProperty Num1Property =
            DependencyProperty.Register("Num1",typeof(TestModel),new PropertyMetadata(0,new PropertyChangedCallback(xxx)));

        static voID xxx(DependencyObject d,DependencyPropertyChangedEventArgs e)         {             MessageBox.Show("sss");         }

总结

以上是内存溢出为你收集整理的Silverlight之MVVM:模型-视图-视图模型(Model-View-ViewModel)(16)全部内容,希望文章能够帮你解决Silverlight之MVVM:模型-视图-视图模型(Model-View-ViewModel)(16)所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存