如何使用包对象中的参数调用工具窗口窗格构造函数?



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

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

对于任何正在寻找如何执行此操作的人,现在有一种更好的方法,您可以在此 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);
}

我不这么认为。

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

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

因此,似乎无法从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();

所以问题是,有没有办法从包对象调用 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);


}

最新更新