WF4设计器数据模型:ModelItem和ModelProperty

简介:

下面是WF设计器结构的基本概述。如下:

image

这里有几个重要组成部分:

  • Source:在VS中,就是XAML,但是这代表了我们正在编辑的持久存储。
  • Instance:代表在内存中正在编辑的条目,对于vs2010中的WF设计器,对应一个System.Activities实例(对象树层次结构)。
  • Model Item Tree :作为View和Instance之间的媒介,并负责对更改通知和跟踪比如撤消状态。
  • Design View:呈现给用户的可视化的编辑视图,这个设计器是用WPF编写的。
  • Metadata Store:类型和设计器之间的映射的一个属性表。

在看上面的图的时候你可能会认为,可以将DesignView直接绑定到Instance,这种方式可以工作,但会有很多问题,最明显的问题是我们的活动类型不会从DependencyObject继承或实现INotifyPropertyChanged的接口。我们也不能指定所有集合都是ObservableCollection(ObservableCollection 已从 WindowsBase.dll 移到 System.dll)。另外,我们要考虑支持一些设计时服务比如撤销/重复,而且在跨越许多对象类型的过程中我们需要确保始终能够处理这些,包括那些不是被我们写的类型。如果我们直接从View到Instance,就将二者仅仅的耦合在一起,会给我们将来做更有趣的事带来不便。例如,我们想要增加更新对象实例的重构支持,我们需要做的不仅仅是对象图,model item tree也需要跟踪很多变化。

The ModelItem tree 

来看一个例子,这个例子和WF的设计器并没有直接的关系,只是来模拟这种方式:

public class Animal
{
    // simple property
    public string Name { get; set; }
    // complex property 
    public Location Residence { get; set; } 
    // list 
    public List<Animal> CloseRelatives { get; set; }
    // dictionary
    public Dictionary<string, object> Features { get; set; } 
}

public class Location
{
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; } 
}
 
EditingContext ec = new EditingContext();
var companion1 = new Animal { Name = "Houdini the parakeet" };
var companion2 = new Animal { Name = "Groucho the fish" };
var animal = new Animal 
                 {
                       Name = "Sasha the pug",
                       Residence = new Location 
                       {
                           StreetAddress = "123 Main Street",
                           City = "AnyTown",
                           State = "Washington"
                       },
                       Features = { 
                          {"noise", "snort" },
                          {"MeanTimeUntilNaps", TimeSpan.FromMinutes(15) }
                       },
                       CloseRelatives = { companion1, companion2 } 
               };
ModelTreeManager mtm = new ModelTreeManager(ec);  mtm.Load(animal);
ModelItem mi = mtm.Root;
 

下图是Animal与ModelItem和ModelProperty的对应关系:

IMAG0111
 
下图可以看到一些具体的属性情况:
image
 
可以通过下面方式来获得相关属性值:
root.Properties["Residence"].
                Value.
                Properties["StreetAddress"].
                Value.GetCurrentValue()
你可能觉得上面的方式比较丑陋,但是在XAML中可以ModelItem.Location.StreetAddress自动路由过来,而其WF团队还承诺正式版会增加Dynamic的支持。
下面是一些测试:

   1:  ModelItem root = mtm.Root;
   2:  Assert.IsTrue(root.GetCurrentValue() == animal, "GetCurrentValue() returns same object");
   3:  Assert.IsTrue(root.ItemType == typeof(Animal),"ItemType describes the item");
   4:  Assert.IsTrue(root.Parent == null,"root parent is null");
   5:  Assert.IsTrue(root.Source == null, "root source is null");
   6:  Assert.IsTrue(((List<Animal>)root.Properties["CloseRelatives"].ComputedValue)[0] == companion1, 
   7:             "ComputedValue of prop == actual object");
   8:  Assert.IsFalse(((List<Animal>)root.Properties["CloseRelatives"].ComputedValue)[0] == companion2, 
   9:             "ComputedValue of prop == actual object");
  10:  Assert.AreEqual(root.Properties["Residence"].
  11:      Value.
  12:      Properties["StreetAddress"].
  13:      Value.GetCurrentValue(), "123 Main Street", "get actual value back out");
  14:  Assert.AreEqual(root, root.Properties["Residence"].Parent, "property points to owner");
  15:  ModelItem location = root.Properties["Residence"].Value;
  16:  Assert.AreEqual(root.Properties["Residence"], location.Source, "sources point to the right place");
 

   1:  EditingContext ec = new EditingContext();
   2:  ModelTreeManager mtm = new ModelTreeManager(ec);
   3:  mtm.Load(new Sequence());
   4:  mtm.Root.Properties["Activities"].Collection.Add(new WriteLine());
本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/20/WF4-ModelItem.html,如需转载请自行联系原作者
相关文章
|
存储 SQL 关系型数据库
postgresql常见命令及操作
  pgsql已经更新到beta11了,不同版本的服务器启动或相关命令、配置可能会有不同,所以得根据pg版本进行操作。下面记录一些工作中常用到的一些操作,主要包括服务启动、备份/恢复数据、数据目录迁移、常见操作命令 本文环境: postgres : v10.3 os: MAC 虽然已经在kong部署中介绍了postgres的部署,为了行文连贯性,这里再简单记录下pg的启动相关命令。
4448 0
|
C#
浅谈WPF的VisualBrush
原文:浅谈WPF的VisualBrush     首先看看VisualBrush的解释,msdn上面的解释是使用 Visual 绘制区域,那么我们再来看看什么是Visual呢?官方的解释是:获取或设置画笔的内容,Visual 是直接继承自DependencyObject,UIElement也是直接继...
2342 2
|
机器学习/深度学习 Java 应用服务中间件
小白第一次在阿里云服务器上部署web项目
做web项目开发的时候,以前都是大牛把框架搭建好,自己往里面写代码。久而久之,小白也想自己搭框架,自己在服务器上部署。所以在本地搭建了一个基于NetBeans+spring+Mybatis+MVC 的web项目框架,搭建过程可参考之前的博客。
8292 0
|
网络安全 开发工具 git
Git常见错误整理
错误1:Git: fatal: The remote end hung up unexpectedly 解决办法: > git config --global http.
4681 0
|
新零售 关系型数据库 数据库
数据库与数据仓库的区别(转载)
数据库:传统的关系型数据库的主要应用,主要是基本的、日常的事务处理,例如银行交易。 数据仓库:数据仓库系统的主要应用主要是OLAP(On-Line Analytical Processing),支持复杂的分析操作,侧重决策支持,并且提供直观易懂的查询结果。
2745 0
|
新零售 安全 大数据
《企业迁云实战》——2.3 阿里云服务简介
本节书摘来自华章计算机《企业迁云实战》一书中的第2章,第2.3节,作者:何强、谭虎、何龙著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。
3788 0
|
iOS开发 Swift
iOS - UIPageViewController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIPageControl : UIControl @available(iOS 2.0, *) public class UIPageControl : UIControl iPhone 和 iPad 都是通过页控件来展示多个桌面,很多 App 在第一次使用时也会使用页控件来介绍自己的功能,页控件的交互效果非常好,适用于把几个简单的页面充分展示出来。
1447 0