如何在WinRT XAML C#中克隆UIElement?

时间:2013-03-22 06:55:26

标签: c# xaml exception windows-runtime uielement

我首先尝试过这种方法,但是收到错误“元素已经是另一个元素的孩子了”

var objClone = new MyImageControl();
objClone = this;
((Canvas)this.Parent).Children.Add(objClone);

然后我检查了thisthis,但是WinRT中没有XamlWriter和XamlReader。我试图使用MemberwiseClone()但它抛出异常,“不能使用已与其基础RCW分离的COM对象。System.Runtime.InteropServices.InvalidComObjectException”。那么有谁能告诉我如何将画布中的现有UserControl克隆到自身?

2 个答案:

答案 0 :(得分:3)

我编写了一个UIElement扩展名,用于复制元素的属性和子元素 - 请注意,为克隆设置事件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using System.Reflection;
using Windows.UI.Xaml.Controls;

namespace UIElementClone
{
    public static class UIElementExtensions
    {
        public static T DeepClone<T>(this T source) where T : UIElement
        {

            T result;

            // Get the type
            Type type = source.GetType();

            // Create an instance
            result = Activator.CreateInstance(type) as T;

            CopyProperties<T>(source, result, type);

            DeepCopyChildren<T>(source, result);

            return result;
        }

        private static void DeepCopyChildren<T>(T source, T result) where T : UIElement
        {
            // Deep copy children.
            Panel sourcePanel = source as Panel;
            if (sourcePanel != null)
            {
                Panel resultPanel = result as Panel;
                if (resultPanel != null)
                {
                    foreach (UIElement child in sourcePanel.Children)
                    {
                        // RECURSION!
                        UIElement childClone = DeepClone(child);
                        resultPanel.Children.Add(childClone);
                    }
                }
            }
        }

        private static void CopyProperties<T>(T source, T result, Type type) where T : UIElement
        {
            // Copy all properties.

            IEnumerable<PropertyInfo> properties = type.GetRuntimeProperties();

            foreach (var property in properties)
            {
                if (property.Name != "Name") // do not copy names or we cannot add the clone to the same parent as the original.
                {
                    if ((property.CanWrite) && (property.CanRead))
                    {
                        object sourceProperty = property.GetValue(source);

                        UIElement element = sourceProperty as UIElement;
                        if (element != null)
                        {
                            UIElement propertyClone = element.DeepClone();
                            property.SetValue(result, propertyClone);
                        }
                        else
                        {
                            try
                            {
                                property.SetValue(result, sourceProperty);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.WriteLine(ex);
                            }
                        }
                    }
                }
            }
        }        
    }
}

如果您发现它有用,请随意使用此代码。

答案 1 :(得分:1)

您可以尝试使用XamlWriter和XamlReader之外的序列化程序来实现链接所描述的相同效果。例如,使用ServiceStack.Text将JSON序列化为字符串,然后从该字符串中获取一个新对象并将其添加到父对象中。