C# Visual Studio Add-in Example
Create new VS Project (Other Project Types/Extensibility/Visual Studio Add-in)
If you get this error when building
Add this to Pre-build event command line:
IF EXIST $(TargetPath).LOCKED(del $(TargetPath).LOCKED)ELSE(IF EXIST $(TargetPath)(move $(TargetPath) $(TargetPath).LOCKED))
Update Add-in location in SampleAddin.AddIn
<Assembly>SampleAddin/SampleAddin.dll</Assembly>
Add this to Post-build event command line to copy your Addin to subfolder
MKDIR "$(ProjectDir)bin\SampleAddin"
XCOPY "$(ProjectDir)bin\SampleAddin.dll" "$(ProjectDir)bin\SampleAddin" /Y /R
DEL "$(ProjectDir)bin\SampleAddin.dll"
DEL "$(ProjectDir)bin\SampleAddin.pdb"
Create New UserControl add add DTE2 property
public partial class SampleUserControl : UserControl
{
public DTE2 DTE { get; set; }
Update Connect.OnConnection method to open UserControl
if (connectMode == ext_ConnectMode.ext_cm_AfterStartup
|| connectMode == ext_ConnectMode.ext_cm_Startup)
{
string controlProgID = "SampleAddin.SampleUserControl";
string guid = "{2C73C576-6153-4a2d-82FE-9D54F4B6AD30}"; //unique per Addin
SampleUserControl obj = (SampleUserControl)Activator.CreateInstance("SampleAddin","SampleAddin.SampleUserControl").Unwrap();
AddIn addIn = _applicationObject.AddIns.Item(1);
EnvDTE80.Windows2 toolWins = (Windows2)_applicationObject.Windows;
Assembly asm = Assembly.GetExecutingAssembly();
object controlWindowRef = null;
Window controlWindow = toolWins.CreateToolWindow2(
addIn,
asm.Location,
controlProgID,
"Sample Addin",
guid,
ref controlWindowRef);
SampleUserControl control = (SampleUserControl)controlWindowRef;
control.DTE = _applicationObject;
controlWindow.Visible = true;
EnvDTE.SolutionEvents solutionEvents = _applicationObject.Events.SolutionEvents;
solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(solutionEvents_Opened);
}
Update Connect.Exec method to reopen your User Control if closed
if (commandName == "ReferencesAddin.Connect.ReferencesAddin")
{
if(!controlWindow.Visible)
{
controlWindow.Visible = true;
}
handled = true;
return;
}
Writing to Output Window from your app
private void LogToOutput(string text){OutputWindow ow = DTE.ToolWindows.OutputWindow;//this code should be in Connect class
//ow.OutputWindowPanes.Add("SampleAddin");
OutputWindowPane owp = ow.OutputWindowPanes.Item("SampleAddin");
ow.Parent.AutoHides = false;
ow.Parent.Activate();owp.Activate();owp.OutputString(text);}
Accessing other tool windows
private void LogToOutput(string text)
{
Window win = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
OutputWindow ow = (OutputWindow)win.Object;
OutputWindowPane owPane;
for (int i = 1; i < ow.OutputWindowPanes.Count; i++)
{
owPane = ow.OutputWindowPanes.Item(i);
owPane.Activate();
owPane.OutputString(text);
}}
Comments
Post a Comment