如何使用 UITypeEditor 为 Visual Studio 创建一个简单的自动化扩展器 [英] How do you create a simple Automation Extender for Visual Studio with UITypeEditor

查看:15
本文介绍了如何使用 UITypeEditor 为 Visual Studio 创建一个简单的自动化扩展器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 中,当您在解决方案资源管理器中选择项目或项目项时,有时您可能希望将自定义属性添加到属性窗口(按 F4 时弹出的窗口).此外,要填写这些属性的值,我需要添加一个按钮来弹出一个表单,这样我就可以在设计时从用户那里收集信息.

In Visual Studio when you select project or project items in the solution explorer there are times when you might want to add custom properties to the properties window(the window that pops up when you press F4). Also, to fill in the values of those properties I need to add a button to pop up a form so I can collect information from the user at design time.

什么是最简单的实现,以便我可以开始?如何使用 UITypeEditAttribute 创建用户界面以某种方式收集值?

What is the simplest implementation of this so I can get started? How would I create a user interface to collect the value some how by using UITypeEditAttribute?

推荐答案

这是我能想到的最简单的实现.

由于这是一个高级主题,因此暗示您在开始实施之前对完成所有步骤感到满意(这些都是常见的编程任务).

Since this is an advanced topic, it is implied that you feel comfortable with completing all the steps before you start the implementation(these are all common programming tasks).

如果有任何不清楚的地方,请发表评论,我会尽量简化.请注意,这被配置为在 Visual Studio 中为 Visual C# 文件创建自定义属性.当您运行或调试 Visual Studio 包并单击任何 .cs 文件时,自定义属性应显示在属性窗口中.提供的评论是必需的说明.

If anything is not clear enough just comment and I will try to simplify. Note that this is configured to create a custom property for a Visual C# file within visual studio. When you run or debug your visual studio package followed by clicking any .cs file, the custom property should show in the properties window. The comments provided are required instructions.

  1. 创建一个 Visual Studio 包.
  2. 创建一个接口来实现您希望添加到属性页的自定义属性.
  3. 创建一个实现自定义属性接口的类,并用属性装饰自定义属性.
  4. 创建实现 IExtenderProvider 接口并覆盖 GetExtenderCanExtend 方法的类.
  5. 创建一个继承自 UITypeEditor 的新类并覆盖 GetEditStyleEditValue 方法.
  1. Create a Visual Studio package.
  2. Create an interface that implements the custom properties that you would like to add to the properties page.
  3. Create a class that implements the custom property interface and decorate the custom property with attributes.
  4. Create class that implements IExtenderProvider interface and override GetExtender and CanExtend methods.
  5. Create a new class that inherits from UITypeEditor and override GetEditStyle and EditValue methods.

让我们开始吧.

1.在 Visual Studio 中创建包.

Package.cs

// ... 
public sealed class ThePackage : Package
{
    private DTE2 Host;
    private ObjectExtenders _extensionManager;
    private MyExtenderProvider _extenderProvider;
    protected override void Initialize()
    {

    Host = (DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE));
    _extenderProvider = new MyExtenderProvider();

    _extenderProviderCookie = Host.ObjectExtenders.RegisterExtenderProvider(VSConstants.CATID.CSharpFileProperties_string,
        "MyExtenderProvider", _extenderProvider);
    }
    protected override void Dispose(bool disposing)
    {
        Host.ObjectExtenders.UnregisterExtenderProvider(_extenderProviderCookie);
        _extenderProvider = null;
        base.Dispose(disposing);
    }
}

<强>2.创建实现所需自定义属性的类.

[ComVisible(true)] // Important!
public interface IMyDynamicExtender
{
    String NewProperty { get; set; }
}

3.创建一个实现自定义属性接口的类.

[ComVisible(true)] // Important!
public class NewPropertyExtender : IMyDynamicExtender, IDisposable
{
    // These attibutes supply the property with some information
    // on how to display and which UITypeEditor to use.
    [DisplayName("New Property")]
    [Category("New")]
    [Description("Specifies the new property")]
    [Editor(typeof(CustomUiTypeEditor), typeof(UITypeEditor))]
    public String NewProperty { get; set; }
    private readonly IExtenderSite _extenderSite;
    private readonly int _cookie;
    private bool _disposed;

    public NewPropertyExtender(IExtenderSite extenderSite, int cookie)
    {
        _extenderSite = extenderSite;
        _cookie = cookie;
    }

    public void Dispose()
    {
        Dispose(true);
        // take the instance off of the finalization queue.
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (_disposed) return;
        if (disposing && _cookie != 0)
        {
            _extenderSite.NotifyDelete(_cookie);
        }
        _disposed = true;
    }
}

4.创建实现 [IExtenderProvider] 接口的类并覆盖 [GetExtender] 和 [CanExtend] 方法.

public class MyExtenderProvider : IExtenderProvider
{
    private IMyDynamicExtender _extender;
    public object GetExtender(string extenderCatid, string extenderName,           
         object extendeeObject, IExtenderSite extenderSite,
        int cookie)
    {
        return _extender = CanExtend(extenderCatid, extenderName, extendeeObject) ?  
            new NewPropertyExtender(extenderSite, cookie) : null;
    }

    public bool CanExtend(string extenderCatid, string extenderName, object extendeeObject)
    {
        // Some implementation will be here in the real world. 
        return true;
    }
} 

5.创建一个继承自 [UITypeEditor] 的新类并覆盖 [GetEditStyle] 和 [EditValue] 方法.

public class CustomUiTypeEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        // Use the result of a dialog or something else here.
        return "HELLO WORLD";
    }
}

这篇关于如何使用 UITypeEditor 为 Visual Studio 创建一个简单的自动化扩展器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆