如何使用UITypeEditor为Visual Studio创建简单的Automation Extender [英] How do you create a simple Automation Extender for Visual Studio with UITypeEditor

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

问题描述

在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 界面和覆盖 GetExtender CanExtend 方法。

  5. 创建一个继承自 UITypeEditor 的新类,覆盖 GetEditStyle EditValue 方法。

  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

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创建简单的Automation Extender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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