克隆WPF控件和对象层次 [英] Cloning WPF controls and object hierarchies

查看:543
本文介绍了克隆WPF控件和对象层次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题克隆的对象hierarchie。这对建模应用的工具包,工具箱包含类的实例作为原型。但我有一个很难克隆这些:)



下面的代码显示了问题:

 公共抽象类形状{
保护名单,LT;的UIElement>要素;
私人帆布油画;
...
公共帆布getCanvas(){...};
}

公共类MovableShape:形状{
保护... propertyA;
私人... propertyXY;

}

公共抽象类AbstractLayout:MovableShape,... {

}

酒店的公共类SomeLayoutClass:AbstractLayout,... {

}

公共类AContainingClass {
SomeLayoutClass布局{搞定;组; }

}

当我插入<的对象code> AContainingClass 到我的项目工作,应该克隆。到目前为止,我试过手动克隆(这将失败,因为在基类中的私人字段)和二进制序列(的BinaryFormatter MemoryStreams )。



第一种方法没有一种方法来调用 base.clone ()办法(还是我错了?),后者不起作用,因为的UIElement 是不是 [Serializable接口]



请注意:它必须是一个深拷贝



任何想法! ? !谢谢





更新



只是为了澄清我的手工克隆的方法:
。如果每个类都有它自己的克隆方法,如何调用基类的克隆的方法?

 公共类Shape {//不是抽象的更多
...
公共形状的clone(){
形状克隆=新Shape(){PropertyA = this.PropertyA,...};
...做一些事情的XamlWriter克隆UI元素...
返回克隆;
}
}

公共类MovableShape:形状{
...
公共MovableShape的clone(){
//如何调用base.Clone?
//这将需要因为我有私人领域进不去!
}
}


解决方案

如果你正试图克隆UI元素,使用的XamlWriter 保存为一个字符串,虽然没有克隆的方法是万无一失的。然后使用 XamlReader 加载一个副本。你可能仍然会碰到的问题。像处理的事情不被复制,X:。名称被复制等,但对于像电网或笔刷简单的元素,它的伟大工程



修改1:有克隆什么没有通用的方法,但是:



1)如果克隆功能编写正确,它会调用基克隆你,并复制基类的东西了。如果没有正确写入调用基克隆不会有太大的帮助,但你可以调用一个私有方法的这样



2)如果你有,你可以使用反射来复制几乎任何东西,甚至单击处理程序,从旧的对象的新对象。



3)的XamlWriter和XamlReader将复制和实例化对象的heirarchies,只是减去某些属性



编辑2:这是一个常见的克隆设计模式在我的类层次我使用。假设基类的造型,派生类圆:

 保护圈(圈T)
{
的copyfrom( T);
}

公众覆盖对象的clone()
{
返回新圈(本);
}

保护无效的copyfrom(圆T)
{
//确保我们有一些复制这也不是一个自我引用
如果(T == NULL || object.ReferenceEquals(T,这一点))
的回报;

//基地
base.CopyFrom((形状)T);

//派生
直径= t.Diameter;
}


I have some problems cloning an object hierarchie. It's a toolkit for modelling applications, the toolbox contains class instances as prototypes. But I'm having a hard time cloning these :)

The following code shows the problem:

public abstract class Shape {
  protected List<UIElement> elements;
  private Canvas canvas;
  ...
  public Canvas getCanvas() { ... };
}

public class MovableShape : Shape {
  protected ... propertyA;
  private ... propertyXY;
  ...
}

public abstract class AbstractLayout : MovableShape, ... {
  ...
}

public class SomeLayoutClass : AbstractLayout, ... {
  ...
}

public class AContainingClass {
  SomeLayoutClass Layout { get; set; }
  ...
}

When I insert an object of AContainingClass into my project worksheet, it should be cloned. So far I tried manual cloning (which fails because of the private fields in the base classes) and binary serialization (BinaryFormatter and MemoryStreams).

The first approach lacks a way to call the base.clone() method (or am I wrong?), the latter does not work because UIElements aren't [Serializable].

Note: it must be a deep copy!

Any ideas? Thanks!


UPDATE

Just to clarify my manual cloning approach: If each class has it's own Clone method, how to call the Clone method of the base class?

public class Shape { // not abstract any more
  ...
  public Shape Clone() {
    Shape clone = new Shape() { PropertyA = this.PropertyA, ... };
    ...do some XamlWriter things to clone UIElements...
    return clone;
  }
}

public class MovableShape : Shape {
  ...
  public MovableShape Clone() {
     // how to call base.Clone??? 
     // this would be required because I have no access to the private fields!
  }
}

解决方案

If you are attempting to clone UIElements, use the XamlWriter to save to a string, though no method of cloning is foolproof. You then use XamlReader to load a copy. You could still run into issues. Things like handlers not being copied, x:Name being duplicated, etc. But for simple elements like Grid or Brush, it works great.

Edit 1: There is no generic way to clone anything, however:

1) If a clone function is written correctly, it will call the base Clone for you, and copy the base class stuff, too. If it isn't written correctly calling the base Clone won't help much, though you can call a private method this way.

2) If you have to, you can use Reflection to copy pretty much anything, even click handlers, from an old object to a new object.

3) XamlWriter and XamlReader will copy and instantiate heirarchies of objects, just minus certain properties.

Edit 2: Here's a common cloning design pattern I use in my class hierarchies. Assume base class shape, derived class circle:

protected Circle(Circle t)
{
    CopyFrom(t);
}

public override object Clone()
{
    return new Circle(this);
}

protected void CopyFrom(Circle t)
{
    // Ensure we have something to copy which is also not a self-reference
    if (t == null || object.ReferenceEquals(t, this))
        return;

    // Base
    base.CopyFrom((Shape)t);

    // Derived
    Diameter = t.Diameter;
}

这篇关于克隆WPF控件和对象层次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆