最近由于项目需要,自己学习了一下WPF,之前接触过sliverlight,所以对理解和编写XAML不是太陌生。其实XAML和html多少还是有点类似的。只不过XAML上添加上了自动binding机制(这里可以对比datagridview控件的binding理解一下)。

WPF有两个核心的东西,也是MVVM等一系列框架的基础。其实说白了,也就是两个重要的接口(INotifyPropertyChanged和ICommand).

INotifyPropertyChanged接口实现了数据和UI的联动,数据改变,UI界面随之改变,反之一样。(这个接口可以理解为监听,生成的Model后,通过ViewModel对Model所有相关者进行监听)

WPF其中一个比较常用的框架MVVM

下面:通过实例来解析一下MVVM(个人的理解,如果有错误,请谅解)

1.MVVM:Model-View-ViewModel

Model:就是各个实体类

View:就是显示的界面,在WPF中一般都是XAML这样的页面

ViewModel:这个是最难理解的。ViewModel官方的解释是连接Model和View的桥梁,封装和暴露一些逻辑处理方法。但个人认为,其实所有的控制和方法都在ViewModel中,可以理解为页面所有方法的一个仓库

2.MVVM的实现原理

MVVM实现简单的来说就是通过ViewModel来连接Model和View实现的。ViewModel实现了接口INotifyPropertyChanged,在Model属性改变时可以触发View页面数据改变。另外,ViewModel中的command实现ICommand这个接口,使得方法也可以直接绑定在UI控件上,省去页面后台书写控件事件的代码

下面实例代码:

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WpfApplication1
{ public class DataItem
{
public int ID { get; set; }
public string Name { get; set; }
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel; namespace WpfApplication1
{
public static class DataDemo
{
private static Collection<DataItem> _DataList = null; public static Collection<DataItem> DataList
{
get
{
if (_DataList == null)
{
_DataList = InitDataList();
}
return _DataList;
}
} private static Collection<DataItem> InitDataList()
{
Collection<DataItem> lists = new Collection<DataItem>();
for (int i = ; i < ; i++)
{
DataItem item = new DataItem();
item.ID = i + ;
item.Name = "例子" + (i + );
lists.Add(item);
}
return lists;
} } }

在Model完成后,编写ViewModel,为UI中使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input; namespace WpfApplication1
{
class QueryDataViewModel:INotifyPropertyChanged
{
#region 变量
/// <summary>
/// 查询的数据
/// </summary>
private Collection<DataItem> _DataList = null;
/// <summary>
/// 查询命令
/// </summary>
private ICommand _QueryCommand = null;
/// <summary>
/// 搜索关键字
/// </summary>
private string _SearchText = string.Empty;
/// <summary>
/// 搜索结果
/// </summary>
private string _SearchResult = string.Empty;
#endregion #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)
{
dataItem = item;
break;
}
}
if (dataItem != null)
{
this.SearchResult = string.Format("ID:{0}\nName:{1}", dataItem.ID, dataItem.Name);
}
}
}
#endregion
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}

在ViewModel中,定义了一系列的参数和方法,其中UI页面是通过ICommand这个实例来调用绑定的方法的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input; namespace WpfApplication1
{
class QueryDataCommand:ICommand
{
private QueryDataViewModel _QueryDataViewModel;
public QueryDataCommand(QueryDataViewModel queryDataViewModel)
{
this._QueryDataViewModel = queryDataViewModel;
} #region ICommand 成员
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public void Execute(object parameter)
{
this._QueryDataViewModel.QueryData();
}
#endregion }
}

编写Command命令,编写前端页面

<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="" d:DesignWidth="">
<Grid x:Name="LayoutRoot">
<Button x:Name="btnSearch" Height="" HorizontalAlignment="Left"
Margin="164,8,0,0" VerticalAlignment="Top" Width="" Content="搜索" Command="{Binding QueryCommand}"/> //这个是绑定的方法,这里的QueryCommand就是ViewModel中的ICommand实例
<TextBox x:Name="txtKeyword" Height="" HorizontalAlignment="Left"
Margin="8,8,0,0" VerticalAlignment="Top" Width="" TextWrapping="Wrap"
d:LayoutOverrides="HorizontalAlignment" Text="{Binding SearchText,Mode=TwoWay}"/>
<TextBox x:Name="txtResult" HorizontalAlignment="Left" Margin="8,36,0,8"
Width="" TextWrapping="Wrap" d:LayoutOverrides="VerticalAlignment" Text="{Binding SearchResult,Mode=OneWay}"/>
</Grid> </UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfApplication1
{ /// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class UserControl1 : UserControl
{
private QueryDataViewModel _QueryDataViewModel = null; public UserControl1()
{
InitializeComponent();
this._QueryDataViewModel = new QueryDataViewModel(DataDemo.DataList);
base.DataContext = this._QueryDataViewModel;
// this.btnSearch.Click += new RoutedEventHandler(btnSearch_Click);
}
void btnSearch_Click(object sender, RoutedEventArgs e)
{
if (this._QueryDataViewModel != null)
{
this._QueryDataViewModel.SearchText = this.txtKeyword.Text;
this._QueryDataViewModel.QueryCommand.Execute(null);
}
} }
}

WPF机制和原理的更多相关文章

  1. logrotate机制与原理[转载]

    http://blog.lightxue.com/how-logrotate-works/ 日志实在是太有用了,它记录了程序运行时各种信息.通过日志可以分析用户行为,记录运行轨迹,查找程序问题.可惜磁 ...

  2. Android(java)学习笔记202:Handler消息机制的原理和实现

     联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系   1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_m ...

  3. Java序列化的机制和原理

    Java序列化的机制和原理 本文讲解了Java序列化的机制和原理.从文中你可以了解如何序列化一个对象,什么时候需要序列化以及Java序列化的算法. 有关Java对象的序列化和反序列化也算是Java基础 ...

  4. Java序列化机制和原理及自己的理解

    Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程.Java序列化API提供一 ...

  5. Session执行机制与原理

    Session执行机制与原理 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] 什么是Session 首先了解一下Session的中文意思:一次会话,什么是一次会话呢?我举个例子:就我们现 ...

  6. Qt核心机制与原理

    转:  https://blog.csdn.net/light_in_dark/article/details/64125085 ★了解Qt和C++的关系 ★掌握Qt的信号/槽机制的原理和使用方法 ★ ...

  7. 为什么要有handler机制?handler机制的原理

    为什么要有handler机制? 在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过 ...

  8. Java序列化机制和原理

    Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程.Java序列化API提供一 ...

  9. Java序列化的机制和原理 转

    转 http://developer.51cto.com/art/200908/147650.htm Java序列化的机制和原理 本文讲解了Java序列化的机制和原理.从文中你可以了解如何序列化一个对 ...

随机推荐

  1. JavaScript:非输入框禁用退格键

    在js文件或<javascript>标签中加入如下代码: /** *非输入框禁用退格键 */ function banBackspace(e) { var ev = e || window ...

  2. node url

    var url = require("url") url模块提供的三个方法: url.parse(urlStr[, parseQueryString][, slashesDenot ...

  3. maven 整合shh框架的pom.xml文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. hadoop学习路线(转)

    刚刚入门hadoop,如何去学习hadoop.google一篇学习路线图,与童鞋们共勉: 转自:http://blog.csdn.net/zhoudaxia/article/details/88017 ...

  5. Linux&nbsp;JDK1.4卸载与1.6的安装

    Linux JDK卸载与安装 一.jdk1.4卸载 Redhat Enterprise 5 中自带安装了jdk1.4,在安装jdk1.6前,把jdk1.4卸载: 1.首先查看系统自带JDK的版本: [ ...

  6. java之异常处理、异常分类、Throwable、自定义异常

    参考http://how2j.cn/k/exception/exception-trycatch/336.html 异常处理 try catch 1.将可能抛出FileNotFoundExceptio ...

  7. Fluuter常遇到的问题

    The ADB binary found at XX is obsolete and has seriousperformance problems with the Android Emulator ...

  8. Spring入门第十四课

    基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...

  9. Thinkphp5+plupload图片上传功能,支持实时预览图片。

    今天和大家分享一个国外的图片上传插件,这个插件支持分片上传大文件.其中著名的七牛云平台的jssdk就使用了puupload插件,可见这个插件还是相当牛叉的. 这个插件不仅仅支持图片上传,还支持大多数文 ...

  10. Deep Learning - Install the Development Environment

    WLS(Windows Subsystem for Linux) Base WLS Installation Guide Initializing a newly installed distro W ...