Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:未序列化只读属性为XAML的属性_Xaml_Serialization_Readonly_Xamlwriter - Fatal编程技术网

WPF:未序列化只读属性为XAML的属性

WPF:未序列化只读属性为XAML的属性,xaml,serialization,readonly,xamlwriter,Xaml,Serialization,Readonly,Xamlwriter,我正在开发一个C#WPF应用程序,它利用Windows窗体PropertyGrid允许用户查看和更改对象的属性。我希望某些属性可见但被锁定,因此我将它们的ReadOnly属性设置为true 但是,在项目的其他地方,我们使用XAML序列化程序来序列化对象,并且发现序列化中忽略了具有只读属性集的属性 为了证明这一点: // Simple class containing 3 properties, one of which has a ReadOnly attribute public class

我正在开发一个C#WPF应用程序,它利用Windows窗体PropertyGrid允许用户查看和更改对象的属性。我希望某些属性可见但被锁定,因此我将它们的ReadOnly属性设置为true

但是,在项目的其他地方,我们使用XAML序列化程序来序列化对象,并且发现序列化中忽略了具有只读属性集的属性

为了证明这一点:

// Simple class containing 3 properties, one of which has a ReadOnly attribute
public class TestClass
{
    public int a { get; set; }
    [ReadOnly(true)]
    public int b { get; set; }
    public int c { get; set; }
}
运行此代码:

TestClass test = new TestClass();

test.a = 1;
test.b = 2;
test.c = 3;

string xStr = XamlWriter.Save(test);

Console.WriteLine(xStr);
给出输出:

<TestClass a="1" c="3" xmlns="clr-namespace:myTest;assembly=myTest" />

很明显,“b”属性缺失


这是正确的行为吗?是否可以序列化ReadOnly设置为true的属性?

如果您很高兴您的输出可能无法反序列化,那么您还可以通过以下方式装饰
[ReadOnly(true)]
属性来实现所需的功能:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

这些属性可能被省略,因为可能无法对它们进行反序列化。我曾预期/希望ReadOnly属性只会影响PropertyGrid显示;“只读”属性同时具有get和set方法,因此可以进行读/写操作。