商信互联
在WinForms应用程序中显示非XAF窗口的最推荐方法是使用自定义视图项将自定义控件嵌入XAF表单(例如DashboardView)中。但是,如果不合适的话,可以通过创建视图并将其控件添加到表单中来将XAF视图嵌入到自定义窗口中。在最简单的情况下,只需创建一个View实例,根据需要对其进行配置,调用其View.CreateControls方法并将其Control添加到窗体中就足够了。
在此示例中,发生动作执行事件时,将调用带有嵌入式XAF ListView的自定义窗口。创建一个新的ViewController后代,并在其构造函数中添加一个新的SimpleAction。在动作的Execute事件处理程序中,创建一个自定义NonXAFForm的新实例,该实例是一个表单对象。该LayoutControl控件创建于布局上正确的地方自定义控件。XAF ListView控件是使用XafApplication.CreateListView方法创建的,其控件是使用CreateControls生成的方法。将控件设置为布局项目,并使用Form.ShowDialog方法调用窗口。
完整的示例项目位于https://github.com/DevExpress-Examples/how-to-show-a-custom-window-with-an-embedded-xaf-view-t483300。
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.BaseImpl;
using DevExpress.XtraLayout;
using System.Windows.Forms;
namespace SolutionWithCustomWindow.Module.Win.Controllers {
public partial class LinkViewController : ViewController {
public LinkViewController() {
SimpleAction showWindowAction = new SimpleAction(this, "Show Window", DevExpress.Persistent.Base.PredefinedCategory.View);
showWindowAction.ImageName = "ModelEditor_Views";
showWindowAction.Execute = showWindowAction_Execute;
}
void showWindowAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
NonXAFForm form = new NonXAFForm();
form.Text = "Form with the XAF ListView";
LayoutControl layoutControl = new LayoutControl();
layoutControl.Dock = System.Windows.Forms.DockStyle.Fill;
form.Controls.Add(layoutControl);
LayoutControlItem item1 = layoutControl.Root.AddItem();
TextBox textBox1 = new TextBox();
item1.Text = "Company";
item1.Control = textBox1;
DevExpress.ExpressApp.View listView = Application.CreateListView(Application.CreateObjectSpace(), typeof(Person), false);
listView.CreateControls();
LayoutControlItem item2 = layoutControl.Root.AddItem();
item2.Text = "Persons";
item2.Control = (Control)listView.Control;
form.ShowDialog();
}
}
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.XtraLayout
Imports System.Windows.Forms
Namespace SolutionWithCustomWindow.Module.Win.Controllers
Partial Public Class LinkViewController
Inherits ViewController
Public Sub New()
Dim showWindowAction As New SimpleAction(Me, "Show Window", DevExpress.Persistent.Base.PredefinedCategory.View)
showWindowAction.ImageName = "ModelEditor_Views"
AddHandler showWindowAction.Execute, AddressOf showWindowAction_Execute
End Sub
Private Sub showWindowAction_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
Dim form As New NonXAFForm()
form.Text = "Form with the XAF ListView"
Dim layoutControl As New LayoutControl()
layoutControl.Dock = System.Windows.Forms.DockStyle.Fill
form.Controls.Add(layoutControl)
Dim item1 As LayoutControlItem = layoutControl.Root.AddItem()
Dim textBox1 As New TextBox()
item1.Text = "Company"
item1.Control = textBox1
Dim listView As DevExpress.ExpressApp.View = Application.CreateListView(Application.CreateObjectSpace(), GetType(Person), False)
listView.CreateControls()
Dim item2 As LayoutControlItem = layoutControl.Root.AddItem()
item2.Text = "Persons"
item2.Control = DirectCast(listView.Control, Control)
form.ShowDialog()
End Sub
End Class
End Namespace
如果需要显示带有“动作”的工具栏并使用“控制器”管理视图的行为,请创建一个嵌套的Frame实例,在其中放置一个View,然后将Frame的模板添加到表单中。
void showWindowAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
NonXAFForm form = new NonXAFForm();
//...
Frame frame = Application.CreateFrame(TemplateContext.NestedFrame);
frame.CreateTemplate();
frame.SetView(listView);
LayoutControlItem item2 = new LayoutControlItem();
item2.Parent = layoutControl.Root;
item2.Text = "Persons";
item2.Control = (Control)frame.Template;
form.ShowDialog();
}
Private Sub showWindowAction_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
Dim form As New NonXAFForm()
'...
Dim frame As Frame = Application.CreateFrame(TemplateContext.NestedFrame)
frame.CreateTemplate()
frame.SetView(listView)
Dim item2 As New LayoutControlItem()
item2.Parent = layoutControl.Root
item2.Text = "Persons"
item2.Control = CType(frame.Template, Control)
form.ShowDialog()
End Sub