感谢您提供复制步骤。我能够重现您的问题。
据我所知,Visual Studio IDE 使用控件而不是表单。
不知道您的表单的预期功能是什么,我只是在下面添加了一个基本示例以开始使用。
很容易有许多其他方法可以做到这一点。我不是 AddIn 开发人员,因此我在该领域的知识有限。
用户控制
首先,右键单击您的项目并添加一个新的用户控件。在我的示例中,我将我的命名为“MyForm”,并在其上放置了一个简单的按钮,单击时显示“Hello”。
此用户控件将成为您的表单。
namespace MyAddin1
{
public partial class MyForm : UserControl
{
public MyForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
}
}
创建表单
我们需要使用托管您的 AddIn 的应用程序和您的 AddIn 实例。
这两个都是您的 AddIn 项目中已声明的成员:_applicationObject 和 _addInInstance。这些是在 OnConnection 事件中设置的。
在下面的代码中,我创建了一个新的工具窗口,在其中托管我的用户控件。我正在使用 Windows2.CreateTooWindow2 方法来做到这一点。
我已将示例代码添加到 Excec 事件中,如下所示。同样,我不确定这是否是合适的地方,但对于演示代码就足够了。
/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
object tempObject = null; // It's required but I'm not sure what one can do with it...
Windows2 windows2 = null; // Reference to the window collection displayed in the application host.
Assembly asm = null; // The assembly containing the user control.
Window myWindow = null; // Will contain the reference of the new Tool Window.
try
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "MyAddin1.Connect.MyAddin1")
{
handled = true;
// Get a reference to the window collection displayed in the application host.
windows2 = (Windows2)_applicationObject.Windows;
// Get the executing assembly.
asm = Assembly.GetExecutingAssembly();
// Create the tool window and insert the user control.
myWindow = windows2.CreateToolWindow2(_addInInstance, asm.Location, "MyAddin1.MyForm", "My Tool Window", "{CB2AE2BD-2336-4615-B0A3-C55B9C7794C9}", ref tempObject);
// Set window properties to make it act more like a modless form.
myWindow.Linkable = false; // Indicates whether the window can be docked with other windows in the IDE or not.
myWindow.IsFloating = true; // Indicates whether the window floats over other windows or not.
// Show the window.
myWindow.Visible = true;
return;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我测试了应用程序,它确实将我的插件添加到了 IDE 的工具菜单中,当我点击我的插件时,它显示了窗口并且它工作正常。显示组装对话框时,它也没有冻结、挂起或任何其他情况。