Can't build UWP in release mode

一曲冷凌霜 提交于 2019-12-01 00:45:53

I also hit a very similar issue. I had a type, let's say Foo, referencing a winmd type Windows.UI.Xaml.Visibility, see below.

    public class Foo
    {
        public Windows.UI.Xaml.Visibility Visibility;
    }

I got the exact same error at compile time when I tried to serialize an instance of type Foo using XmlSerializer. It seems that .Net Native tools have problem with generating XmlSerializer for types like Foo.

Here's my workaround. I created my own Visibility type, MyVisibility, and changed the existing Visibility field to a get only property (so that XmlSerializer would not serialize the property).

    public class Foo
    {
        public Windows.UI.Xaml.Visibility Visibility
        {
            get
            {
                return (Visibility)myVisibility;
            }
        }

        public MyVisibility myVisibility;
    }

    public enum MyVisibility
    {
        Visible = 0,
        Collapsed = 1
    }

Here's my test code for serializing a Foo instance,

    public static void Test()
    {
        var foo = new Foo();
        foo.myVisibility = MyVisibility.Collapsed;            
        var serializer = new XmlSerializer(typeof(Foo));
        using (var stream = new MemoryStream())
        {
            serializer.Serialize(stream, foo);
            stream.Position = 0;
            var foo1 = (Foo)serializer.Deserialize(stream);
            Assert.True(foo.Visibility == foo1.Visibility);
        }
    }

Hope this helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!