[英]How to Use Visual Studio Resource Selector Dialog in Custom Designer
将.NET 4.0与Visual Studio 2017和Visual Basic .NET结合使用(也可以在C#中完成),我创建了一个WinForms应用程序。 作为应用程序的一部分,我通过添加一个新类并从System.Windows.Forms.Control继承来创建了一个自定义控件。
Public Class MyControl
Inherits System.Windows.Forms.Control
End Class
如果将自定义控件添加到窗体,则可以使用“属性窗口”添加BackgroundImage。 在“属性”窗口中,如果单击BackgroundImage属性,它将显示一个省略号按钮。 单击该按钮将打开“选择资源对话框”窗口。
现在,我通过继承System.Windows.Forms.Design.ControlDesigner为控件创建了一个自定义设计器。 我还创建了设计器表单,当在设计视图中双击控件时,该表单就会弹出。 在设计器表单上,我希望能够使用上面显示的来自Visual Studio的相同“选择资源”对话框来选择背景图像。 我一直找不到“选择资源”对话框的位置。我怀疑是在以下程序集中,但没有找到它。
Microsoft.VisualStudio.Design.dll
有人可以告诉我Visual Studio使用的“选择资源”对话框的全限定名称空间以及它存在于哪个程序集中吗?
您可以在设计时使用代码显示所有属性的属性编辑器。 为此,您可以在System.Design
程序EditorServiceContext
找到内部的EditorServiceContext
,然后通过传递控件设计器,要编辑的控件和要编辑的属性名称来调用其EditValue
方法。
例
您可以在此存储库中找到示例的完整源代码:
该示例的核心部分是设计器类:
using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms.Design;
public class MyControlDesigner : ControlDesigner
{
DesignerVerbCollection verbs;
public override DesignerVerbCollection Verbs
{
get
{
if (verbs == null)
{
verbs = base.Verbs;
verbs.Add(new DesignerVerb("Show Select Resource",
(s, e) => ShowSelectResource()));
}
return verbs;
}
}
public void ShowSelectResource()
{
var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
.Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
var editValue = editorServiceContext.GetMethod("EditValue",
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
editValue.Invoke(null, new object[] { this, this.Component, "SomeProperty" });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.