-1

我正在创建一个Visual Studio 2013使用Managed Package Framework( MPF) 的自定义项目解决方案。我已经成功地为我的项目文件节点创建了自定义节点类型并公开了 custom 。FileNodeProperties

在 MPF 中,我发现我可以公开一个类型为集合的属性,FileNodeProperties这样当项目运行并在解决方案资源管理器中选择文件节点时,该属性将显示在属性窗口中,并带有尾随[...]单击时显示标准集合编辑器对话框的按钮。

[ComVisible(true)]
[CLSCompliant(false)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("x-x-x-x-x")] // obfuscated
internal class ModelFileNodeProperties : FileNodeProperties
{
...
[LocDisplayName("Things")] 
public List <int>Things { get; set; }
...
}

我的问题是:

我可以创建我自己的表单/编辑器,当单击[...]以获得更复杂的属性类型时将显示它,VS 会以某种方式查询的表单?我需要一个比标准属性编辑器中提供的更丰富的 UI。想想WinForm如果单击属性上的[...]Font会发生什么;或MessageQueue在表单中放置一个项目,然后单击Path属性。

例如在本例中,我希望在编辑属性时获得更丰富的编辑体验

[LocDisplayName("SomethingComplex")]
public SomethingComplexWithLotsOfConfig SomethingComplex { get; set; } 

我在网上搜索过各种强积金网站,例如

......但遗憾的是无济于事。我怀疑这是我在某处应用的 COM 管理接口?

4

1 回答 1

0

回答我自己的问题——经过多次试验;检查强积金的项目源代码;和一些谷歌搜索;我发现必须使用Editor属性装饰作为属性公开的类,并创建一个从UITypeEditor派生的自定义编辑器。通常在WinForms其中找到可以在其他地方使用,包括它看起来的自定义项目系统。

例如,这是在我们的项目系统中代表“模型”文件的文件节点。(一些代码,虽然这里不重要,但已被省略)。下面Plugins的属性是我想要一个[...]按钮的属性。

[ComVisible(true)]
[CLSCompliant(false)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("xx-xx-xx-xx-xx")]
internal class ModelFileNodeProperties : MbtFileNodeProperties
{
    #region Constructor

    /// <summary>
    ///     Initializes a new instance of the <see cref="ModelFileNodeProperties" /> class.
    /// </summary>
    /// <param name="node">The node.</param>
    public ModelFileNodeProperties(HierarchyNode node) : base(node)
    {
        Plugins = new Plugins(node); // Our "model" node has a concept of "plug-ins"
    }

    #endregion

    #region Properties

    [LocDisplayName("Plugins")]
    public Plugins Plugins { get; set; } // <----- I wan't a [...] button for this property 
    ...
}

...这是代表我们模型文件的“插件”的类。注意Editor属性:

[Editor(typeof (PluginsEditor), typeof (UITypeEditor))]  // <---- MPF will spot this and expose the editor to Visual Studio
internal class Plugins 
{
    #region Fields

    private readonly HierarchyNode _node;

    public Plugins(HierarchyNode node) { _node = node; }

    // this prevents the object type being displayed in the Property window
    public override string ToString() { return "No plug-ins installed"; }

    public string Something { get; set; }

    public string OrOther { get; set; }

    [Microsoft.VisualStudio.Project.LocDisplayName("Things")]
    public List<int> Things { get; set; }

    #endregion

    ...
}

这是用于呈现和修改Plugins类型的编辑器。

internal class PluginsEditor : UITypeEditor
{
    public PluginsEditor()
    {

    }

    #region Methods

    /// <summary>
    ///     Edits the specified object's value using the editor style indicated by the
    ///     <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> method.
    /// </summary>
    /// <returns>
    ///     The new value of the object. If the value of the object has not changed, this should return the same object it was
    ///     passed.
    /// </returns>
    /// <param name="context">
    ///     An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain
    ///     additional context information.
    /// </param>
    /// <param name="provider">An <see cref="T:System.IServiceProvider" /> that this editor can use to obtain services. </param>
    /// <param name="value">The object to edit. </param>
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        var form = new PluginsForm();
        form.ShowDialog();  // we want to show a modal dialog

        return value;
    }

    /// <summary>
    ///     Gets the editor style used by the
    ///     <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)" /> method.
    /// </summary>
    /// <returns>
    ///     A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle" /> value that indicates the style of editor used by the
    ///     <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)" /> method. If the
    ///     <see cref="T:System.Drawing.Design.UITypeEditor" /> does not support this method, then
    ///     <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> will return
    ///     <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None" />.
    /// </returns>
    /// <param name="context">
    ///     An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain
    ///     additional context information.
    /// </param>
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal; // we don't want a drop-down but rather a modal dialog
    }


    #endregion
}
于 2014-11-01T01:43:19.123 回答