LightSwitch中内置了新增和编辑的功能,一种是在表格或列表窗口的功能区点击新增等操作,这时会弹出一个模式窗口。但这有个问题就是对于界面元素没办法进行排版。如有多行文本框,在这个窗口中只能是单行。另一种是增加一个新增界面,然后调用该界面,但此时不是模式窗口,而是打开一个新的选项卡进行新增。
以上二种模式各有优缺点,本文讨论的是如何在主窗口中实现新增的同时,可以对新增界面进行元素排列。
LightSwitch中提供的布局中有模式窗口这种方式,我们可以从这方面来着手,在主界面中设置一个模式窗口,同时将该窗口不可见,同时也不显示按钮。这样可以等我们需要进调用该模式窗口。而下面的新增布局,则可以进行自由发挥了。
模式窗口中布局,在此就不在介绍了,可根据自己的要求进行处理。
下面是如何调用该模式窗口,来显示新增界面。首先切换工程到文件视图,定义一个模式窗口处理类
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.LightSwitch.Client; using Microsoft.LightSwitch; using Microsoft.LightSwitch.Presentation; using Microsoft.LightSwitch.Presentation.Extensions; using System.ComponentModel; using Microsoft.LightSwitch.Threading; namespace LightSwitchApplication { public class ModalWindowHelper { private IVisualCollection _collection; private string _dialogName; private string _entityName; private IScreenObject _screen; private IContentItemProxy _window; private IEntityObject _entity; public ModalWindowHelper(IVisualCollection visualCollection, string dialogName, string entityName = "") { _collection = visualCollection; _dialogName = dialogName; _entityName = ((entityName != "") ? entityName : _collection.Details.GetModel().ElementType.Name); _screen = _collection.Screen; } public void InitializeUI() { _window = _screen.FindControl(_dialogName); _window.ControlAvailable += (object s, ControlAvailableEventArgs e) => { var window = (ChildWindow)e.Control; window.Closed += (object s1, EventArgs e1) => { DialogClosed(s1); }; }; } public bool CanAdd() { return (_collection.CanAddNew == true); } public bool CanEditSelected() { return (_collection.SelectedItem != null); } public void AddEntity() { _window.DisplayName = string.Format("新增 {0}", _entityName); _collection.AddNew(); OpenModalWindow(); } public void EditSelectedEntity() { _window.DisplayName = string.Format("编辑 {0}", _entityName); OpenModalWindow(); } private void OpenModalWindow() { _entity = _collection.SelectedItem as IEntityObject; _screen.OpenModalWindow(_dialogName); } public void DialogOK() { if (_entity != null) { _screen.CloseModalWindow(_dialogName); } } public void DialogCancel() { if (_entity != null) { _screen.CloseModalWindow(_dialogName); DiscardChanges(); } } public void DialogClosed(object sender) { var window = (ChildWindow)sender; if (window.DialogResult.HasValue == false) { DiscardChanges(); } } private void DiscardChanges() { if (_entity != null) { _entity.Details.DiscardChanges(); } } } }2.重写主窗体中关于要新增的表格或列表的新增\编辑\删除代码,如下图,主要是canexecute 及execute代码。
请分别重写相关代码,但需要注意的是,deptDialogHelper = new ModalWindowHelper(Deptment, "DeptEditDialog","部门信息"); DeptEditDialog为模式窗口名。
下面运行来看一下效果如何。