How to call ToolWindowPane constructor with parameters from Package object?

1.2k Views Asked by At

I am creating a Visual Studio Package extension with a tool window that needs to be shown. I have a class representing my tool window that extends ToolWindowPane:

[Guid(GuidList.guidToolWindowPersistanceString)]
public class MyToolWindow : ToolWindowPane
{
    public MyToolWindow() :
        base(null)
    {
        // Set the window title
        this.Caption = "ToolWindowName";

        // Set content of ToolWindow
        base.Content = new MyControl();
    }
}

where MyControl is a WPF object to be hosted inside the tool window. This parameterless constructor is called from the Packageclass when calling the method Package.CreateToolWindow:

[ProvideToolWindow(typeof(MyToolWindow))]
public sealed class MyPackage : Package
{
   //... Package initialization code...

    private void ShowMainToolWindow()
    {
        var window = (ToolWindowPane)CreateToolWindow(typeof(MyToolWindow), 0); //how to pass parameters in tool window constructor??

        if ((null == window) || (null == window.Frame))
            throw new NotSupportedException(Resources.CanNotCreateWindow);

        IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
        Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
    }       

So the question is, is there any way to call a non-parameterless constructor of a ToolWindowPane from the Package object?

4

There are 4 best solutions below

1
On BEST ANSWER
0
On

So it seems like there's no way of calling a non-parameterless constructor from the Package object. The workaround I adopted is to create public properties or methods inside MyToolWindowobject and call them from the Package object like this:

var window = (ToolWindowPane)CreateToolWindow(typeof(MyToolWindow),0);

if ((null == window) || (null == window.Frame))
        throw new NotSupportedException(Resources.CanNotCreateWindow);

((MyToolWindow)window).Property1 = ...
((MyToolWindow)window).Property2 = ...
((MyToolWindow)window).ExecuteMethod();
2
On

So the question is, is there any way to call a non - parameterless constructor of a ToolWindowPane from the Package object?

If you want to pass parameter to your WPF control, you could create a parameter constructor in your WPF control. like this:

public partial class MyControl: UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MyControl"/> class.
        /// </summary>
        public MyControl()
        {
            this.InitializeComponent();            
        }

        public MyControl(List<User> users)
        {
            this.InitializeComponent();
            dgUsers.ItemsSource = users;
        }

Then you could pass parameter via constructor like this:

public MyToolWindow() : base(null)
        {
            this.Caption = "TestToolWindow";
            List<User> users = new List<User>();
            users.Add(new User() { Id = 1, Name = "Test Xu", Birthday = new DateTime(1971, 7, 23) });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
            users.Add(new User() { Id = 3, Name = "Jack Doe", Birthday = new DateTime(1991, 9, 2) });
            this.Content = new MyControl(users);




    }
0
On

For anyone looking for how to do this, there is now a much better way, which you can see in this AsyncToolWindow sample.

By overriding 3 methods in your AsyncPackage class, you can define some state which will be passed to the tool window when it's initialised:

public override IVsAsyncToolWindowFactory GetAsyncToolWindowFactory(Guid toolWindowType)
{
    return toolWindowType.Equals(Guid.Parse(SampleToolWindow.WindowGuidString)) ? this : null;
}

protected override string GetToolWindowTitle(Type toolWindowType, int id)
{
    return toolWindowType == typeof(SampleToolWindow) ? SampleToolWindow.Title : base.GetToolWindowTitle(toolWindowType, id);
}

protected override async Task<object> InitializeToolWindowAsync(Type toolWindowType, int id, CancellationToken cancellationToken)
{
    // Perform as much work as possible in this method which is being run on a background thread.
    // The object returned from this method is passed into the constructor of the SampleToolWindow 
    var dte = await GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;

    return new SampleToolWindowState
    {
        DTE = dte
    };
}

By adding a parameterised constructor to your ToolWindowPanel class, you can then receive the passed state:

// "state" parameter is the object returned from MyPackage.InitializeToolWindowAsync
public SampleToolWindow(SampleToolWindowState state) : base()
{
    Caption = Title;
    BitmapImageMoniker = KnownMonikers.ImageIcon;

    Content = new SampleToolWindowControl(state);
}