最近由于项目需要,自己学习了一下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. AtCoder Beginner Contest 103

    https://beta.atcoder.jp/contests/abc103 A - Task Scheduling Problem Time Limit: 2 sec / Memory Limit ...

  2. 【Lintcode】062.Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  3. [转载]Windows网络编程系列教程之四:Select模型

    原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...

  4. 洛谷P1220关路灯——区间DP

    题目:https://www.luogu.org/problemnew/show/P1220 区间DP. 代码如下: #include<iostream> #include<cstd ...

  5. foreach中collection的三种用法

    转载:http://blog.sina.com.cn/s/blog_b0d90e8c0102v1q1.html 传参参考:http://www.cnblogs.com/ruiati/p/6410339 ...

  6. 【原】SQLPLUS支持上下翻页

    作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/p/4191650.html 适用:RHE ...

  7. OpenCPN介绍及编译

    OpenCPN介绍及编译 OpenCPN是一个航海应用软件系统,采用wxWidgets界面框架,支持OpenGL,可以跨平台运行在Windows , Linux , Mac电脑上. OpenCPN是一 ...

  8. [Oracle]oracle查询表列名、及列数

    --查询表列数 select count( column_name ) from user_tab_columns where table_name = 'CJ_HOME_MEDICAL_RECORD ...

  9. 安装pywin32时,出现找不到python27注册信息的解决办法

    1. 检查一下注册表是否存在python其它版本的信息 方法: 1)在命令行中输入regedit打开注册表 2)在HKEY_CURRENT_USER\Software中找一下是否存在python注册信 ...

  10. 从扫码支付想到的超级APP主宰一切,数据!数据!还是数据!

    前言 做室内定位的人其实内心都明白:基于指纹方法的移动端定位,无论paper每年出来多少,距离真正的大规模应用的距离还有多么遥远.指纹采集,指纹更新,似乎在生产实践上就是不可能的难题.所有还在基于人工 ...