Add an Action that Displays a Pop-up Window 添加显示弹出窗口按钮

In this lesson, you will learn how to create an Action that shows a pop-up window. This type of Action is useful when you want a user to input several parameters in a pop-up dialog before an Action is executed.

在本课中,您将学习如何创建显示弹出窗口的操作。当您希望用户在执行操作之前在弹出对话框中输入多个参数时,这种类型的操作非常有用。

Note 注意

Before proceeding, take a moment to review the following lessons.

  • Set a Many-to-Many Relationship (XPO/EF)
  • Add a Simple Action

在继续之前,请花点时间复习以下课程。

  • 设置多对多关系 (XPO/EF)
  • 添加简单操作

Create a Controller and a PopupWindowShowAction

创建控制器和弹出窗口窗口显示操作

  • Add a new View Controller to the MySolution.Module project, as described in the Add a Simple Action lesson. Name it PopupNotesController.
  • Right-click the MySolution.Module | Controllers | PopupNotesController.cs (PopupNotesController.vb) file, and choose View Designer to invoke the Designer.

  • 向 MySolution.Module 项目添加新的视图控制器,如"添加简单操作"一课中所述。将其命名为"弹出笔记控制器"。

  • 右键单击"我的解决方案"模块 |控制器 |PopupNotesController.cs (PopupNotesController.vb) 文件,然后选择"视图设计器"以调用设计器。

    popup_notes_designer

  • Drag the PopupWindowShowAction component from the DX.19.2: XAF Actions tab to the Designer. In the popupWindowShowAction1 "Properties" window, set the Name and Id properties to "ShowNotesAction", and set the Caption property to "Show Notes". Set the Category property to "Edit". This property specifies the Action group to which the current Action belongs. All Actions within a single group are displayed together sequentially in a UI.

  • 将"弹出窗口显示操作"组件从 DX.19.2:XAF 操作选项卡拖动到设计器。在弹出窗口WindowShowAction1"属性"窗口中,将"名称"和"ID"属性设置为"显示笔记操作",并将标题属性设置为"显示注释"。将"类别"属性设置为"编辑"。此属性指定当前操作所属的操作组。单个组中的所有操作在 UI 中按顺序一起显示。

    Tutorial_EF_Lesson4_2

  • To activate the PopupNotesController for DemoTask Detail Views only, set the Controller's ViewController.TargetObjectType property to MySolution.Module.DemoTask, and set the ViewController.TargetViewType to DetailView.

  • 要仅激活演示任务详细信息视图的 PopupNotes 控制器,请将控制器的视图控制器.TargetTotos 类型属性设置为 MySolution.module.demoTask,并将视图控制器.TargetViewType设置为详细信息视图。

    Tutorial_EF_Lesson4_2_1

Specify the Popup List View

指定弹出列表视图

Focus the ShowNotesAction component. In the Properties window, switch to the Events view. Double-click the CustomizePopupWindowParams event, add the "using" ("Imports" in VB) directive according to your ORM, and replace the auto-generated event handler code with the following code.

聚焦"显示笔记操作"组件。在"属性"窗口中,切换到"事件"视图。双击"自定义PopupWindowParams"事件,根据您的ORM在VB中添加"使用"("导入")指令,并将自动生成的事件处理程序代码替换为以下代码。

using DevExpress.Persistent.BaseImpl; //For XPO
using DevExpress.Persistent.BaseImpl.EF; //For EF
// ...
private void ShowNotesAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs args) {
    IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Note));
    string noteListViewId = Application.FindLookupListViewId(typeof(Note));
    CollectionSourceBase collectionSource = Application.CreateCollectionSource(objectSpace, typeof(Note), noteListViewId);
    args.View = Application.CreateListView(noteListViewId, collectionSource, true);
    //Optionally customize the window display settings.
    //args.Size = new System.Drawing.Size(600, 400);
    //args.Maximized = true;
    //args.IsSizeable = false;
}

For details on this event, refer to the PopupWindowShowAction.CustomizePopupWindowParams topic. The code above will create the Notes List View when generating the pop-up window.

To create a List View, use the XafApplication object again (as you did in the previous lesson). In the code above, the XafApplication helps you find the ID of the required List View in the Application Model. Note that a collection source for the List View is created in a separate Object Space. To create the Object Space, use XafApplication again.

有关此事件的详细信息,请参阅弹出窗口窗口显示操作。生成弹出窗口时,上述代码将创建"备注列表视图"。

要创建列表视图,请再次使用 XafApplication 对象(如上一课所示)。在上面的代码中,Xaf 应用程序可帮助您在应用程序模型中找到所需列表视图的 ID。请注意,列表视图的集合源是在单独的对象空间中创建的。要创建对象空间,请再次使用 Xaf 应用程序。

Handle the Execute Event

处理执行事件

In the Controller's Designer, switch to the Events view in the Properties window with the Action's properties. Double-click the Execute event, add the "using" ("Imports" in VB) directive and replace the auto-generated event handler code with the following code.

在控制器的设计器中,切换到"属性"窗口中的"事件"视图,该视图具有操作的属性。双击 Execute 事件,在 VB 中添加"使用"("导入")指令,并将自动生成的事件处理程序代码替换为以下代码。

using MySolution.Module.BusinessObjects;
// ...
private void ShowNotesAction_Execute(object sender, PopupWindowShowActionExecuteEventArgs args) {
    DemoTask task = (DemoTask)View.CurrentObject;
    foreach(Note note in args.PopupWindowViewSelectedObjects) {
        if(!string.IsNullOrEmpty(task.Description)) {
            task.Description += Environment.NewLine;
        }
        task.Description += note.Text;
    }
    if(((DetailView)View).ViewEditMode == ViewEditMode.View) {
        View.ObjectSpace.CommitChanges();
    }
}
  • The Execute event is raised when clicking the OK button in the pop-up window. When the handler above is executed, the Text property value of the selected Note object will be appended to the Task.Description property value.
  • In this code, access the object selected in the pop-up window by using the event handler's PopupWindowShowActionExecuteEventArgs.PopupWindowViewSelectedObjects parameter.
  • To refresh the editor representing the modified Description property, first find its PropertyEditor in the current View's CompositeView.Items collection using the CompositeView.FindItem method. To update the value displayed by the Property Editor's editor, call the PropertyEditor.ReadValue method.
  • In ASP.NET Web applications, Detail Views are displayed in View and Edit modes. When the ShowNotes Action is activated for a DemoTask Detail View that is in View mode, the changes made to the DemoTask.Description property should be saved to the database. For this purpose, the CommitChanges method of the current View's ObjectSpace is called. When you use the ShowNotes Action in a DemoTask Detail View that is in Edit mode, the changes can be saved or rolled back via the corresponding built-in Actions.
  • 单击弹出窗口中的"确定"按钮时引发"执行"事件。执行上述处理程序时,所选 Note 对象的 Text 属性值将追加到 Task.描述属性值。
  • 在此代码中,使用事件处理程序的"弹出窗口显示操作执行事件"参数访问弹出窗口中选定的对象。
  • 要刷新表示已修改的"描述"属性的编辑器,请首先在当前视图的"复合视图.项"集合中使用"复合视图.FindItem"方法查找其属性编辑器。要更新属性编辑器的编辑器显示的值,请调用属性编辑器.ReadValue 方法。
  • 在ASP.NET Web 应用程序中,详细信息视图显示在"查看"和"编辑"模式下。当为处于"视图"模式的演示任务详细信息视图激活 ShowNotes 操作时,对 DemoTask.描述属性所做的更改应保存到数据库中。为此,将调用当前视图的对象空间的提交更改方法。在处于编辑模式的演示任务详细信息视图中使用 ShowNotes 操作时,可以通过相应的内置操作保存或回滚更改。

Add Notes to the UI

向 UI 添加注释

  • To add the Note business class to the UI construction process, add it to the Application Model.
  • If your ORM is Entity Framework, you should register the Note type in DbContext before proceeding to the next step. Edit the BusinessObjectsMySolutionDbContext.cs file as shown below.

  • 要将 Note 业务类添加到 UI 构造过程,请将其添加到应用程序模型中。

  • 如果您的 ORM 是实体框架,则应在 DbContext 中注册 Note 类型,然后再继续执行下一步。编辑业务对象_MySolutionDbContext.cs 文件,如下所示。

    public class MySolutionDbContext : DbContext {
        //...
        public DbSet<Note> Notes { get; set; } 
    }
  • To add the Note business class from the Business Class Library use the Module Designer. Double-click the Module.cs (Module.vb) file within the MySolution.Module project. The Exported Types section of the designer lists the business classes that can be added.

  • 要从商务舱库中添加 Note 业务类,请使用模块设计器。双击 MySolution.模块项目中的Module.cs(模块.vb)文件。设计器的"导出类型"部分列出了可添加的业务类。

    • If you are using XPO:

      Locate the Referenced Assemblies | DevExpress.Persistent.BaseImpl.v19.2 | Note node.

    • 如果您使用的是 XPO:
      找到引用的程序集 |开发快车.持久.BaseImpl.v19.2 |注释节点。
    • If you are using Entity Framework:

      Locate the Referenced Assemblies | DevExpress.Persistent.BaseImpl.EF.v19.2 | Note node.

    • 如果您使用的是实体框架:
      找到引用的程序集 |开发快车.持久.BaseImpl.EF.v19.2 |注释节点。
  • Select this node and press the SPACEBAR, or right-click it and choose Use Type in Application in the invoked context menu.

         选择此节点并按空格键,或右键单击它,并在调用的上下文菜单中选择"在应用程序中使用类型"。

Tutorial_EF_Lesson4_4_1

  • Build the project.

  • 生成项目。

    Rebuilding_Solution

    Note 注意

    To create Note objects, you should add the Note item to the New Action's items. To do this, perform the steps demonstrated in the Add an Item to the New Action lesson.

    To access the existing Note objects, add the Note item to the ShowNavigationItem Action's items (displayed by the navigation control). To do this, perform the steps demonstrated in the Add an Item to the Navigation Control lesson.

    要创建 Note 对象,应将"注释"项添加到"新建操作"项中。为此,执行"将项目添加到新操作"一课中演示的步骤。
    要访问现有的 Note 对象,将"注释"项添加到"显示导航项"操作的项目(由导航控件显示)。为此,执行"将项目添加到导航控制"一课中演示的步骤。

Result 结果

Run the WinForms or ASP.NET application. Create several Note objects via the New Action. Select the Task item in the navigation control. Double-click one of the listed Task objects. In the invoked detail form, find the Show Notes toolbar button that represents the implemented Action. Click this button, which will invoke a pop-up window. Select a Note object in the list and click OK. Check to see that the Task.Description property value has been changed.

运行 WinForms 或ASP.NET应用程序。通过"新建"操作创建多个 Note 对象。在导航控件中选择"任务"项。双击列出的任务对象之一。在调用的详细信息窗体中,查找表示已实现操作的"显示注释"工具栏按钮。单击此按钮,这将调用弹出窗口。在列表中选择"注意"对象,然后单击"确定"。检查任务描述属性值已更改。

Tutorial_EF_Lesson4_5

Tip 提示

For an example of how to create and show a Detail View, refer to the How to: Create and Show a Detail View of the Selected Object in a Popup Window topic.

有关如何创建和显示详细信息视图的示例,请参阅"如何:在弹出窗口"主题中创建和显示选定对象的详细信息视图。

You can view the code demonstrated in this tutorial in the MySolution.Module | Controllers | PopupNotesController.cs (PopupNotesController.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkMainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
您可以查看本教程中 MySolution.模块中演示的代码。控制器 |PopupNotesController.cs (PopupNotes控制器.vb) 文件的主演示安装与 XAF.主演示应用程序安装在%PUBLIC%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkMainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/

.

原文地址:https://www.cnblogs.com/foreachlife/p/Add-an-Action-that-Displays-a-Pop-up-Window.html