Customize the Application UI and Behavior(自定义应用程序UI和行为)
In XAF, the data model defines the database structure and UI. Changes to your entity classes affect the UI. For example, if you add a new property to an entity class, a new editor appears in the corresponding List and Detail views.
在XAF中,数据模型定义了数据库结构和UI。对实体类的更改会影响UI。例如,如果向实体类添加新属性,则会在相应的List和Detail视图中显示新的编辑器。
You can use the auto-generated UI or customize it according to your business requirements and scenarios. This topic describes how to customize your application’s appearance and behavior.
您可以使用自动生成的UI或根据您的业务需求和场景对其进行自定义。本主题介绍如何自定义应用程序的外观和行为。
Customize the Application UI Metadata(自定义应用程序UI元数据)
Use Attributes in Code(在代码中使用属性)
You can use built-in attributes to edit the Application Model, create controls, and customize the application’s appearance and behavior. For example, you can validate field content, change field visibility, or format displayed data with one line of code.
您可以使用内置属性来编辑应用程序模型、创建控件以及自定义应用程序的外观和行为。例如,您可以使用一行代码验证字段内容、更改字段可见性或格式化显示的数据。
Follow the steps below to replace the Quote property’s single-line editor with a multi-line editor.
按照以下步骤将Quote属性的单行编辑器替换为多行编辑器。
1.Apply the FieldSizeAttribute attribute to the Quote property in the Testimonial class and pass Unlimited as the attribute’s parameter.
将FieldSizeAtoral属性应用于Testimonial类中的Quote属性,并将Unlimited作为属性的参数传递。
C#
// ...
namespace SimpleProjectManager.Module.BusinessObjects {
// ...
public class Testimonial {
[FieldSize(FieldSizeAttribute.Unlimited)]
public virtual string Quote { get; set; }
// ...
}
}
2.Run the application and open the Testimonial Detail View. The Quote property editor now supports multi-line input:
运行应用程序并打开Testimonial Detail View。Quote属性编辑器现在支持多行输入:
ASP.NET Core Blazor
Windows Forms
Use the Model Editor(使用模型编辑器)
XAF exports data model settings to the application’s metadata (Application Model). If you do not want to define the application’s UI structure and behavior in your data model code, edit the application metadata in the Model Editor. Each project stores metadata settings as XML markup in XAMFL files. These files form the Application Model’s layered structure.
XAF将数据模型设置导出到应用程序的元数据(应用程序模型)。如果不想在数据模型代码中定义应用程序的UI结构和行为,请在模型编辑器中编辑应用程序元数据。每个项目都将元数据设置存储为XAMFL文件中的XML标记。这些文件构成应用程序模型的分层结构。
Follow the steps below to change the Customer object’s caption in the Model Editor:
按照以下步骤在模型编辑器中更改客户对象的标题:
1.In the SimpleProjectManager.Module project, double-click the Model.DesignedDiffs.xafml file to open it in the Model Editor.
在SimpleProjectManager模块项目中,双击Model. DesignedDiffs.xafml文件以在模型编辑器中打开它。
2.In the Model Editor node tree, navigate to the BOModel | SimpleProjectManager.Module.BusinessObjects | Customer node and set the ObjectCaptionFormat property to {0:FullName}.
在模型编辑器节点树中,导航到BOModel | SimpleProjectManager.Module.BusinessObjects | Customer节点,并将ObjectCaptionFormat属性设置为{0:FullName}。
3.Run the application. The caption of the Customer Detail View now displays the FullName property value.
运行应用程序。客户详细信息视图的标题现在显示FullName属性值。
ASP.NET Core Blazor
Windows Forms
Define Custom Logic and UI Elements(定义自定义逻辑和UI元素)
You can use the Model Editor and built-in attributes to change UI element and control options. Alternatively, you can create Controllers and Actions in code to replace the application’s default UI elements or implement custom business logic.
您可以使用模型编辑器和内置属性来更改UI元素和控件选项。或者,您可以在代码中创建控制器和操作来替换应用程序的默认UI元素或实现自定义业务逻辑。
A Controller is a component you use to change application flow, customize UI elements, and implement custom user interaction. Controllers can include Actions. XAF renders Actions in the UI as interactive elements, such as buttons or menu items.
Controller是用于更改应用程序流程、自定义UI元素和实现自定义用户交互的组件。控制器可以包含Actions。XAF将UI中的Actions呈现为交互式元素,例如按钮或菜单项。
Follow the steps below to implement a SimpleAction that allows users to mark the selected task as completed and sets the EndDate property of the ProjectTask object to the current date and time:
按照以下步骤实现SimpleAction,允许用户将选定的任务标记为已完成,并将ProjectTask对象的EndDate属性设置为当前日期和时间:
1.In the Solution Explorer, go to the MySolution.Module project, right-click the Controllers folder, and choose Add DevExpress Item | New Item… from the context menu to invoke the Template Gallery. Select the XAF Controllers | View Controller Visual Studio template, specify ProjectTaskController as the new item’s name, and click Add Item.
在解决方案资源管理器中,转到MySolutions. Module项目,右键单击控制器文件夹,然后从上下文菜单中选择Add DevExpress Item | New Item…以调用模板库。选择the XAF Controllers | View Controller Visual Studio template,将ProjectTaskController指定为新项目的名称,然后单击添加项目。
2.Visual Studio displays an autogenerated ProjectTaskController.cs file with a View Controller declaration. Add the following code to the controller constructor:
Visual Studio显示带有View Controller声明的自动生成的ProjectTaskController. cs文件。将以下代码添加到控制器构造函数:
C#
using DevExpress.ExpressApp;
using SimpleProjectManager.Module.BusinessObjects;
namespace SimpleProjectManager.Module.Controllers {
public class ProjectTaskController : ViewController {
public ProjectTaskController() {
// Specify the type of objects that can use the Controller.
TargetObjectType = typeof(ProjectTask);
// Activate the Controller in any type of View.
TargetViewType = ViewType.Any;
SimpleAction markCompletedAction = new SimpleAction(this, "MarkCompleted",
DevExpress.Persistent.Base.PredefinedCategory.RecordEdit) {
TargetObjectsCriteria =
(CriteriaOperator.FromLambda<ProjectTask>(t => t.Status != ProjectTaskStatus.Completed)).ToString(),
ConfirmationMessage =
"Are you sure you want to mark the selected task(s) as 'Completed'?",
ImageName = "State_Task_Completed"
};
markCompletedAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
markCompletedAction.Execute += (s, e) => {
foreach (ProjectTask task in e.SelectedObjects) {
task.EndDate = DateTime.Now;
task.Status = ProjectTaskStatus.Completed;
View.ObjectSpace.SetModified(task);
}
View.ObjectSpace.CommitChanges();
View.ObjectSpace.Refresh();
};
}
}
// ...
}
In the code above, the Object Space’s IObjectSpace.CommitChanges method commits changes to the database. An Object Space entity is an ORM-independent implementation of the Repository and Unit Of Work design patterns. Object Space allows you to query or modify data in the transaction. Refer to the following help topic for information on other Object Space methods: Create, Read, Update and Delete Data.
在上面的代码中,Object Space的IObjectSpace.CommitChanges方法向数据库提交更改。Object Space实体是Repository和Unit Of Work设计模式的与ORM无关的实现。Object Space允许您查询或修改事务中的数据。有关其他Object Space方法的信息,请参阅以下帮助主题:创建、读取、更新和删除数据。
Run the application and mark the selected task as completed.
运行应用程序并将选定的任务标记为已完成。
ASP.NET Core Blazor
Windows Forms
When you apply this action to multiple objects, it iterates through the selected objects, modifies their properties, commits changes to the database, and refreshes the screen.
当您将此操作应用于多个对象时,它会遍历选定的对象、修改它们的属性、将更改提交到数据库并刷新屏幕。
Next Lesson(下一课)
Reuse Implemented Functionality
重用实现的功能