2

我正在创建一个带有需要显示的工具窗口的 Visual Studio 包扩展。我有一个代表我的工具窗口的类,它扩展了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();
    }
}

MyControl要在工具窗口中托管的 WPF 对象在哪里。Package调用方法时从类中调用此无参数构造函数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());
    }       

ToolWindowPane所以问题是,有没有办法从对象中调用 a 的非无参数构造Package函数?

4

4 回答 4

1

对于正在寻找如何执行此操作的任何人,现在有一个更好的方法,您可以在此AsyncToolWindow 示例中看到。

通过覆盖AsyncPackage类中的 3 个方法,您可以定义一些在初始化时将传递给工具窗口的状态

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
    };
}

通过向您的ToolWindowPanel类添加参数化构造函数,您可以接收传递的状态:

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

    Content = new SampleToolWindowControl(state);
}
于 2019-07-23T01:44:01.167 回答
0

所以似乎没有办法从Package对象调用非无参数构造函数。我采用的解决方法是在对象内部创建公共属性或方法MyToolWindow,并从对象中调用它们,Package如下所示:

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();
于 2017-07-21T13:59:31.233 回答
0

我不这么认为。

这里没有任何内容表明它是:

https://docs.microsoft.com/en-us/visualstudio/extensibility/registering-a-tool-window https://www.mztools.com/articles/2015/MZ2015004.aspx

于 2017-07-13T15:27:37.960 回答
-1

那么问题来了,有没有办法从 Package 对象中调用 ToolWindowPane 的非无参数构造函数?

如果要将参数传递给 WPF 控件,可以在 WPF 控件中创建参数构造函数。像这样:

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;
        }

然后你可以像这样通过构造函数传递参数:

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);




    }
于 2017-07-14T08:35:31.887 回答