.NET 6 WinForms自定义控件智能标签不显示的解决方法
.NET 6 WinForms自定义控件智能标签无法显示的配置问题
我已实现一个自定义控件,为使其正常工作需扩展设计时支持并创建智能标签。我创建了继承自ControlDesigner的自定义Designer类、DesignerActionList,并将DesignerActionList正确添加至CustomDesigner::ActionLists,但切换到WinForms设计器时,无法看到智能标签面板。无论尝试何种方法都未成功,即使按照文档示例实现的自定义控件ColorLabel,其智能标签也无法显示。
我使用的是**.NET 6 WinForms应用程序**,严格遵循文档示例的步骤操作,可参考以下示例代码复现问题。
自定义控件及其Designer的示例代码
[Designer(typeof(ColorLabelDesigner))] public class ColorLabel : Label { private bool colorLockedValue = false; public bool ColorLocked { get { return colorLockedValue; } set { colorLockedValue = value; } } public override Color BackColor { get { return base.BackColor; } set { if (ColorLocked) return; else base.BackColor = value; } } public override Color ForeColor { get { return base.ForeColor; } set { if (ColorLocked) return; else base.ForeColor = value; } } } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] public class ColorLabelDesigner : ControlDesigner { private DesignerActionListCollection actionLists; public override DesignerActionListCollection ActionLists { get { if (null == actionLists) { actionLists = new DesignerActionListCollection(); actionLists.Add( new ColorLabelActionList(this.Component)); } return actionLists; } } } public class ColorLabelActionList : System.ComponentModel.Design.DesignerActionList { private ColorLabel colLabel; private DesignerActionUIService designerActionUISvc = null; public ColorLabelActionList(IComponent component) : base(component) { this.colLabel = component as ColorLabel; this.designerActionUISvc = GetService(typeof(DesignerActionUIService)) as DesignerActionUIService; } private PropertyDescriptor GetPropertyByName(String propName) { PropertyDescriptor prop; prop = TypeDescriptor.GetProperties(colLabel)[propName]; if (null == prop) throw new ArgumentException( "Matching ColorLabel property not found!", propName); else return prop; } public Color BackColor { get { return colLabel.BackColor; } set { GetPropertyByName("BackColor").SetValue(colLabel, value); } } public Color ForeColor { get { return colLabel.ForeColor; } set { GetPropertyByName("ForeColor").SetValue(colLabel, value); } } public bool LockColors { get { return colLabel.ColorLocked; } set { GetPropertyByName("ColorLocked").SetValue(colLabel, value); this.designerActionUISvc.Refresh(this.Component); } } public String Text { get { return colLabel.Text; } set { GetPropertyByName("Text").SetValue(colLabel, value); } } public void InvertColors() { Color currentBackColor = colLabel.BackColor; BackColor = Color.FromArgb( 255 - currentBackColor.R, 255 - currentBackColor.G, 255 - currentBackColor.B); Color currentForeColor = colLabel.ForeColor; ForeColor = Color.FromArgb( 255 - currentForeColor.R, 255 - currentForeColor.G, 255 - currentForeColor.B); } public override DesignerActionItemCollection GetSortedActionItems() { DesignerActionItemCollection items = new DesignerActionItemCollection(); //Define static section header entries. items.Add(new DesignerActionHeaderItem("Appearance")); items.Add(new DesignerActionHeaderItem("Information")); //Boolean property for locking color selections. items.Add(new DesignerActionPropertyItem("LockColors", "Lock Colors", "Appearance", "Locks the color properties.")); if (!LockColors) { items.Add(new DesignerActionPropertyItem("BackColor", "Back Color", "Appearance", "Selects the background color.")); items.Add(new DesignerActionPropertyItem("ForeColor", "Fore Color", "Appearance", "Selects the foreground color.")); items.Add(new DesignerActionMethodItem(this, "InvertColors", "Invert Colors", "Appearance", "Inverts the fore and background colors.", true)); } items.Add(new DesignerActionPropertyItem("Text", "Text String", "Appearance", "Sets the display text.")); StringBuilder location = new StringBuilder("Location: "); location.Append(colLabel.Location); StringBuilder size = new StringBuilder("Size: "); size.Append(colLabel.Size); items.Add(new DesignerActionTextItem(location.ToString(), "Information")); items.Add(new DesignerActionTextItem(size.ToString(), "Information")); return items; } }
核心问题
我的问题并非“如何创建智能标签”,而是“如何为自定义控件正确配置智能标签使其显示?”我推测缺少某些配置,但此前无设计时开发经验。
内容的提问来源于stack exchange,提问作者jtxkopt - STOP GENOCIDE
相关产品推荐
相关产品推荐

