现在的位置: 首页 > 综合 > 正文

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

2011年02月28日 ⁄ 综合 ⁄ 共 3014字 ⁄ 字号 评论关闭

下面是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());

抱歉!评论已关闭.