ICustomTypeDescriptor 和 XAML 序列化

发布于 2024-12-06 11:54:56 字数 2050 浏览 0 评论 0原文

我有一个可以通过 PropertyGrid 进行编辑的业务对象。它包含标签列表(假设为简化字符串)。

public class MyObject
{
    // bunch of properties here, cut for readiness
    public LabelsList List {get;set;}
    // ...
}

标签列表是从 List 继承的简单类:

public class LabelsList : List<T>
{}

为了在属性网格中正确显示我的对象(我的意思是可扩展的可编辑标签列表),我为 LabelsList 实现了 ICustomTypeDescriptor,显着更改了 GetProperties() 方法:

public PropertyDescriptorCollection GetProperties()
{
    var props = new PropertyDescriptorCollection(null);
    for (var i = 0; i < this.Count; i++)
    {            
        var descriptor = new LabelsListPropertyDescriptor(this, i);
        props.Add(descriptor);
    }
    return props;
}

现在问题是- 当我通过在基础类型上调用 XamlWriter.Save(this) 来使用标准 XAML 序列化时,它会在生成的 XAML 中添加过多的 LabelsList.LabelName 标记:

<wpfdl:LabelsList>
    *<wpfdl:LabelsList.Label1Name>*
        <wpfdl:Label LabelText="Label1Name"/>
    *</wpfdl:LabelsList.Label1Name>*
...
</wpfdl:LabelsList>

这实际上会禁用以下内容(MyObject)XamlReader.Parse(exportedXaml) 调用,因为标签名称可以包含特殊字符。实现对象的正确编辑和序列化的正确解决方法是什么?提前致谢。
更新
通过更改相应的 PropertyDescriptor 来消除不必要的标签:

public override bool ShouldSerializeValue(object component)
{
    return false;
}

生成的 xaml 如下(原语是我的对象自定义类型的名称):

<Primitive>
    <Primitive.Labels>
        <Label LabelText="text1" LabelPosition="position1" />
        <Label LabelText="text2" LabelPosition="position2" />
    </Primitive.Labels>
</Primitive>

差不多就是这样,但现在内的标签缺失:

'Collection property 'WPF_DrawingsTest.Primitive'.'Labels' is null.' Line number '1' and line position '96'.

仍然需要完成这项工作。也许测试项目将有助于了解我正在寻找的内容:
测试项目,100 kb,无病毒

I have a business object that can be edited via PropertyGrid. It contains list of labels (let's say strings for simplification).

public class MyObject
{
    // bunch of properties here, cut for readiness
    public LabelsList List {get;set;}
    // ...
}

List of labels is simple class inherited from List :

public class LabelsList : List<T>
{}

For proper displaying of my object in property grid (by that i mean expandable editable list of labels too) i've implemented an ICustomTypeDescriptor for LabelsList, changing notably GetProperties() method :

public PropertyDescriptorCollection GetProperties()
{
    var props = new PropertyDescriptorCollection(null);
    for (var i = 0; i < this.Count; i++)
    {            
        var descriptor = new LabelsListPropertyDescriptor(this, i);
        props.Add(descriptor);
    }
    return props;
}

Now the problem - when i use standard XAML serialization by calling XamlWriter.Save(this) on underlying type, it adds excessive LabelsList.LabelName tag inside resulting XAML :

<wpfdl:LabelsList>
    *<wpfdl:LabelsList.Label1Name>*
        <wpfdl:Label LabelText="Label1Name"/>
    *</wpfdl:LabelsList.Label1Name>*
...
</wpfdl:LabelsList>

That actually disables following (MyObject)XamlReader.Parse(exportedXaml) call because label names can contain special characters. What is proper workaround to achieve both correct editing and serializing of objects? Thanks in advance.
update
Made unnecessary tags go away by changing respective PropertyDescriptor :

public override bool ShouldSerializeValue(object component)
{
    return false;
}

Resulting xaml is as follows (Primitive is name of my object custom type):

<Primitive>
    <Primitive.Labels>
        <Label LabelText="text1" LabelPosition="position1" />
        <Label LabelText="text2" LabelPosition="position2" />
    </Primitive.Labels>
</Primitive>

That's pretty much it, but now <LabelsList> tags inside <Primitive.Labels> are missing :

'Collection property 'WPF_DrawingsTest.Primitive'.'Labels' is null.' Line number '1' and line position '96'.

Still need to make this work. Maybe test project will help to see what i'm looking for :
Test project, 100 kb, no viruses

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-12-13 11:54:56

重构了初始业务对象,序列化/反序列化现在自动完成并且工作正常。

Refactored initial business object, serialization/deserialization now is done automatically and works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文