我有一个ProjectViewForm
,一个ProjectView
和一个Project
。由于缺少初始化和数据,ProjectView.OnLoad
方法没有被调用,并且在尝试显示ProjectViewForm
时导致应用程序崩溃。
如何获取用ProjectView
数据初始化的Project
对象,即如何获得OnLoad
的呼叫?
[SmartPart]
public partial class ProjectViewForm : Form
{
public ProjectViewForm()
{
InitializeComponent(); // gets here
}
protected override void OnLoad(System.EventArgs eventArgs)
{
System.Diagnostics.Trace.WriteLine(""); // doesn't get here
}
}
public partial class ProjectView : System.Windows.Forms.UserControl
{
public ProjectView()
{
InitializeComponent();
...
}
protected override void OnLoad(System.EventArgs eventArgs)
{
this._presenter.OnViewReady();
base.OnLoad(eventArgs);
... populate with Project data ...
}
}
Project
是一个POCO,其数据填充了ProjectView
在ProjectViewForm.Designer.cs中,实例化一个新的Project
和ProjectView
:
partial class ProjectViewForm
{
private void InitializeComponent()
{
Project project1 = new Project();
System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(ProjectViewForm));
this.projectView1 = new ProjectView();
this.SuspendLayout();
this.projectView1.SomeComponent1 = null;
this.projectView1.SomeComponent2 = null;
...
project1.SomeProperty1 = null;
project1.SomeProperty2 = null;
...
this.Controls.Add(this.projectView1);
this.ResumeLayout(false);
}
private ProjectView projectView1;
}
在Controller中,我有以下代码:
WorkItem.State["CurrentProject"] = project; // Project to display
string key = System.String.Concat("ProjectViewForm", project.Id);
ProjectViewForm projectViewForm = WorkItem.Items.Get<ProjectViewForm>(key); // calls ProjectViewForm constructor
if (projectViewForm == null)
{
projectViewForm = WorkItem.Items.AddNew<ProjectViewForm>(key);
}
else
{
projectViewForm.Focus();
}
projectViewForm.Enter += ProjectViewForm_Enter;
projectViewForm.Leave += ProjectViewForm_Leave;
string formTitle = System.String.Concat (project.Name, " - Status: ", project.ProjectStatusType.Text);
var info = new Microsoft.Practices.CompositeUI.WinForms.WindowSmartPartInfo();
info.Location = System.Windows.Forms.Cursor.Position;
... yadda yadda yadda ...
projectViewForm.Dock = System.Windows.Forms.DockStyle.Fill;
WorkItem.Workspaces[Infrastructure.Interface.Constants.WorkspaceNames.WindowsWorkspace].Show (projectViewForm, info);