[英]How to call ToolWindowPane constructor with parameters from Package object?
我正在創建帶有需要顯示的工具窗口的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.CreateToolWindow
時,從Package
類中調用此無參數構造函數:
[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
的非參數構造函數?
所以問題是,有什么辦法可以從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);
}
因此,似乎無法從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();
對於正在尋找方法的任何人,現在有了更好的方法,您可以在此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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.