1

実際にはコレクション(TextDecorationCollection)である依存関係プロパティに指定できるxamlシリアル化属性はありますか?

非常に大きくて複雑なオブジェクトのクローンを作成するためにシリアル化を使用したいと思います。ここに、簡略化されたコードのサンプルがあります。

MyVisualObjectがあり、カスタムフォントなど、クローンを作成したい多くのプロパティが含まれています。

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class Export : Attribute
{
}

public class MyVisualObject : DependencyObject
{
    [Export]
    public CustomFont Font
    {
        get { return (CustomFont)GetValue(FontProperty); }
        set { SetValue(FontProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Font.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FontProperty =
        DependencyProperty.Register("Font", typeof(CustomFont), typeof(MyVisualObject));

    public MyVisualObject()
    {
        this.Font = new CustomFont();
    }
}

そして、カスタムフォントは次のように定義されます。

public class CustomFont : DependencyObject
    {
        public TextDecorationCollection Decorations
        {
            get { return (TextDecorationCollection)GetValue(DecorationsProperty); }
            set { SetValue(DecorationsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextDecorations.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DecorationsProperty =
            DependencyProperty.Register("Decorations", typeof(TextDecorationCollection), typeof(CustomFont), new UIPropertyMetadata(new TextDecorationCollection()));

        public CustomFont()
        {
            this.Decorations = System.Windows.TextDecorations.Underline;
        }
    }

ディープクローン方式:

public static T DeepClone<T>(T from)
        {
            object clone = Activator.CreateInstance(from.GetType());

            Type t = from.GetType();
            System.Reflection.PropertyInfo[] pinf = t.GetProperties();

            foreach (PropertyInfo p in pinf)
            {
                bool serialize = false;

                foreach (object temp in p.GetCustomAttributes(true))
                {
                    if (temp is Export)
                    {
                        serialize = true;
                    }
                }

                if (serialize)
                {
                    string xaml = XamlWriter.Save(p.GetValue(from, null));                        
                    XmlReader rd = XmlReader.Create(new StringReader(xaml));
                    p.SetValue(clone, XamlReader.Load(rd), null);
                }                
            }

            return (T)clone;
        }

問題は、装飾を下線として初期化するたびに

this.Decorations = System.Windows.TextDecorations.Underline;

クローン作成プロセスが次のエラーでクラッシュします:「タイプ'System.Windows.TextDecorationCollection'のコレクションに値を追加すると例外がスローされました。」行番号「1」および行位置「213」。

私が知る限り、この部分であるシリアル化

string xaml = XamlWriter.Save(p.GetValue(from, null));

装飾がコレクションとして設定されていないxamlを返します。

<CustomFont xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <CustomFont.Decorations>
        <av:TextDecoration Location="Underline" /> 
    </CustomFont.Decorations>
</CustomFont>

ただし、xamlが次のようになっている場合、クローンプロセスは機能します。

<CustomFont xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <CustomFont.Decorations>
        <av:TextDecorationCollection>
            <av:TextDecoration Location="Underline" /> 
        </av:TextDecorationCollection>
    </CustomFont.Decorations>
</CustomFont>

回避策を見つけました。文字列を置き換えるものです。

xaml = xaml.Replace("<CustomFont.Decorations><av:TextDecoration Location=\"Underline\" /></CustomFont.Decorations>", "<CustomFont.Decorations><av:TextDecorationCollection><av:TextDecoration Location=\"Underline\" /></av:TextDecorationCollection></CustomFont.Decorations>");

しかし、それは本当に汚いと思います。もっとクリーンなソリューションを提供できれば幸いです(たとえば、Decorationsプロパティの属性を指定します)

4

1 に答える 1

0

次の属性を Decorations プロパティに適用してみましたか:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
于 2011-12-14T14:33:35.177 に答える