6

最初にこのアプローチを試しましたが、「要素はすでに別の要素の子です」というエラーが発生しました

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

それからこれこれを確認しましたが、XamlWriterとXamlReaderはWinRTでは利用できません。MemberwiseClone()を使用しようとしましたが、「基礎となる RCW から分離された COM オブジェクトは使用できません。System.Runtime.InteropServices.InvalidComObjectException」という例外がスローされます。それで、キャンバス内の既存の UserControl をそれ自体に複製する方法を教えてもらえますか?

4

2 に答える 2

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);
                            }
                        }
                    }
                }
            }
        }        
    }
}

このコードが役立つ場合は、自由に使用してください。

于 2013-05-20T08:00:52.167 に答える
1

XamlWriter と XamlReader 以外のシリアライザーを試して、リンクで説明されているのと同じ効果を得ることができます。たとえば、ServiceStack.Text を使用してオブジェクトを文字列に JSON シリアル化し、その文字列から新しいオブジェクトを取得して親に追加します。

于 2013-03-22T07:30:14.260 に答える