【发布时间】:2017-04-13 18:37:38
【问题描述】:
我正在关注 this C# article 来学习如何创建 ActionList 和 Action Items,但是本文只关注 DesignerActionPropertyItem...
我想创建一个DesignerActionMethodItem 类型的项目来调用必须打开MultilineStringEditor 来编辑控件的文本行的方法,这与我们在默认 中看到的操作项目相同RichTextBox 控件:
谁能解释我如何在 C# 或 VB.NET 中做到这一点?
我坚持传递给UITypeEditor.EditValue() 方法的值,我认为这是调用/显示编辑器的方法,但我不确定我必须将哪个值传递给第一个参数(它接受IServiceProvider 或 ITypeDescriptorContext)。我见过 this related answer 但我认为应该存在比创建实现 IServiceProvider 和 ITypeDescriptorContext 的类更直接/更简单的方法...因为我将运行特定的 UITypeEditor (MultilineStringEditor)。
这是我目前得到的;当我单击“编辑文本行...”操作项时,没有任何反应,任何异常,什么都没有;我不确定这是一个好信号还是坏信号,因为如果我尝试将其他类型的值传递给UITypeEditor.EditValue() 方法的第一个参数,那么当我单击我的自定义操作项时会出现无效类型转换的异常。
C#代码版本:
public class MyControlActionList : DesignerActionList {
private DesignerActionUIService designerActionUISvc;
public new MyControl Component {
get { return (MyControl)base.Component; }
}
public MyControlActionList(MyControl component) : base(component) {
// Cache a reference to DesignerActionUIService, so the DesigneractionList can be refreshed.
this.designerActionUISvc = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
}
public override DesignerActionItemCollection GetSortedActionItems() {
DesignerActionItemCollection items = new DesignerActionItemCollection();
items.Add(new DesignerActionMethodItem(this, "EditTextLines", "Edit Text Lines...", "Behavior", "Opens the Lines collection editor", false));
return items;
}
public void EditTextLines(){
PropertyDescriptor pd = TypeDescriptor.GetProperties(this.Component)("Text");
MultilineStringEditor editor = (MultilineStringEditor)pd.GetEditor(typeof(UITypeEditor));
editor.EditValue((IServiceProvider)this.GetService(typeof(MultilineStringEditor)), this.Component.Text);
}
}
VB.NET 代码版本:
Public Class MyControlActionList : Inherits DesignerActionList
Private designerActionUISvc As DesignerActionUIService
Public Shadows ReadOnly Property Component As MyControl
Get
Return DirectCast(MyBase.Component, MyControl)
End Get
End Property
Public Sub New(ByVal component As MyControl)
MyBase.New(component)
' Cache a reference to DesignerActionUIService, so the DesigneractionList can be refreshed.
Me.designerActionUISvc = DirectCast(GetService(GetType(DesignerActionUIService)), DesignerActionUIService)
End Sub
Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
Dim items As New DesignerActionItemCollection()
items.Add(New DesignerActionMethodItem(Me, "EditTextLines", "Edit Text Lines...", "Behavior", "Opens the Lines collection editor", False))
Return items
End Function
Public Sub EditTextLines()
Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(Me.Component)("Text")
Dim editor As MultilineStringEditor = DirectCast(pd.GetEditor(GetType(UITypeEditor)), MultilineStringEditor)
editor.EditValue(CType(Me.GetService(GetType(MultilineStringEditor)), IServiceProvider), Me.Component.Text)
End Sub
End Class
【问题讨论】:
-
ElektroStudios,只是根据我自己在该领域的冒险经历提出的一些建议。您正在努力寻找好的示例,而这些示例在互联网上很难找到。Reference Source 上提供了有关 .Net 类的一些信息,但设计器代码大多不完整。然而,如果你给自己一个像ILSpy 这样的反编译器,你可以很容易地看到 MS 是如何做到的。您需要的东西在 System.Design.dll 中。给一个人一个汉堡,他就有饭吃;教他如何宰牛,他就会有很多肉。
-
Plutonix 我认为您混淆了编辑器类名称,或者我可能不太了解您,我为 RichTextbox 文本获得的默认编辑器是 MultilineStringEditor 类;然后回答您的问题:我正在尝试使用该编辑器,因为它是 RichTextBox 的默认编辑器。 StringCollectionEditor 类用于编辑字符串集合(例如ListBox.Items),而不是用于“原始”文本。 @TnTinMn 感谢您的建议!
标签: c# .net vb.net winforms windows-forms-designer