首页 > 解决方案 > Visual Studio 2019 设计器,在设计器中设置数组属性会导致弹出“属性值无效”

问题描述

我正在使用 Visual Studio 2019 版本 16.8.2 我有一个 .net Core 5 用户库。为了诊断转换为 Core 5 的现有项目中的问题,我创建了一个简单的用户控件:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinFormsControlLibrary2
{
    public partial class UserControl1 : UserControl
    {
        private TwoElems[] mynums = { new TwoElems(), new TwoElems() };
        public UserControl1()
        {
            InitializeComponent();
        }
        [Browsable(true), NotifyParentProperty(true), EditorBrowsable(EditorBrowsableState.Always)]
        [Category("Log control parameters")]
        [DisplayName("array property")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TwoElems[] MyNums
        {
            get { return mynums; }
            set { mynums = value; }
        }
    }
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class TwoElems
    {
        [Browsable(true), NotifyParentProperty(true), EditorBrowsable(EditorBrowsableState.Always)]
        public int sz { get; set; }
        public TwoElems()
        {
            sz = 30;
        }
    }
}

这编译得很好,并且包含此用户控件的项目被添加到工具箱中。然后将控件添加到窗体中,单击省略号 (...) 打开 MyNums 的子属性页面,显示项目。

更改任一项目的值会导致属性表中的弹出窗口显示:属性值无效并且详细信息为:对象引用未设置为对象的实例。

单击确定关闭弹出窗口并重新检查数组属性显示更改的值写回属性。

这种代码(数组属性)在 .net 框架和/或更早版本的 Visual Studio 下运行良好。

关于如何确定是什么原因造成的任何想法?

问候,奥兹

标签: c#user-controlsvisual-studio-2019

解决方案


推荐阅读