Add a Parametrized Action(添加参数化操作)
This lesson explains how to add a Parametrized Action. The Parametrized Action displays an editor that allows users to type in a parameter value before they run the action.
本课介绍如何添加参数化操作。参数化操作显示一个编辑器,允许用户在运行操作之前输入参数值。
The instructions below demonstrate how to add an action that searches for a DemoTask object by its Subject property value and displays the Detail View of the found object.
下面的说明演示了如何添加一个操作,该操作通过其主题属性值搜索DemoTask对象并显示找到的对象的详细信息视图。
Note
Before you proceed, take a moment to review the previous lessons:
在继续之前,请花点时间回顾一下之前的课程:
- Implement a Data Model: Basics
- Extend the Data Model
- Initialize Entity Properties
- Add a Simple Action
Step-by-Step Instructions(分步说明)
1.Add a new View Controller to the MySolution.Module project, as described in the Add a Simple Action lesson. Name it FindBySubjectController.
将新的视图控制器添加到MySolutions. Module项目,如添加简单操作课程中所述。将其命名为FindBySubjectController。
2.In the MySolution.Module | Controllers | FindBySubjectController.cs file, specify the controller’s properties:
在MySolutions. Module|Controller|FindBySubjectController.cs文件中,指定控制器的属性:
C#
// ...
using MySolution.Module.BusinessObjects;
// ...
public partial class FindBySubjectController : ViewController {
public FindBySubjectController() {
InitializeComponent();
// Activate the controller only in the List View.
TargetViewType = ViewType.ListView;
// Activate the controller only for root Views.
TargetViewNesting = Nesting.Root;
// Specify the type of objects that can use the controller.
TargetObjectType = typeof(DemoTask);
}
// ...
}
For more information about the root View, see the following topic: IsRoot.
有关根视图的更多信息,请参阅以下主题:IsRoot。
3.Add a Parametrized Action to the Controller:
向控制器添加参数化操作:
C#
public partial class FindBySubjectController : ViewController {
public FindBySubjectController() {
InitializeComponent();
TargetViewType = ViewType.ListView;
TargetViewNesting = Nesting.Root;
TargetObjectType = typeof(DemoTask);
ParametrizedAction findBySubjectAction =
new ParametrizedAction(this, "FindBySubjectAction", PredefinedCategory.View, typeof(string)) {
ImageName= "Action_Search",
NullValuePrompt = "Find task by subject..."
};
findBySubjectAction.Execute += FindBySubjectAction_Execute;
}
// ...
}
When a user submits a string in the Action’s editor, the Action’s ParametrizedAction.Execute event fires.
当用户在Action的编辑器中提交字符串时,Action的ParametrizedAction. Execute事件会触发。
4.Handle the Action’s Execute event to implement custom code:
处理Action的Execute事件以实现自定义代码:
C#
public partial class FindBySubjectController : ViewController {
public FindBySubjectController() {
// ...
findBySubjectAction.Execute += FindBySubjectAction_Execute;
}
private void FindBySubjectAction_Execute(object sender, ParametrizedActionExecuteEventArgs e) {
var objectType = ((ListView)View).ObjectTypeInfo.Type;
IObjectSpace objectSpace = Application.CreateObjectSpace(objectType);
string paramValue = e.ParameterCurrentValue as string;
object obj = objectSpace.FirstOrDefault<DemoTask>(task => task.Subject.Contains(paramValue));
if(obj != null) {
DetailView detailView = Application.CreateDetailView(objectSpace, obj);
detailView.ViewEditMode = ViewEditMode.Edit;
e.ShowViewParameters.CreatedView = detailView;
}
}
// ...
}
For details on the event handler implementation, refer to the Detailed Explanation section.
有关事件处理程序实现的详细信息,请参阅详细说明部分。
5.Run the application.
运行应用程序。
Select the Task item in the navigation control. Type a word from an existing task’s Subject into the Find Task by Subject editor and press Enter. The application displays a detail form with this task.
在导航控件中选择任务项。从现有任务的主题中键入一个单词到“按主题查找任务”编辑器中,然后按Enter。应用程序显示包含此任务的详细信息表单。
ASP.NET Core Blazor
Windows Forms
Detailed Explanation(详细说明)
Search Implementation(搜索实现)
In XAF, you use the Object Space to query and update persistent objects. Call the static XafApplication.CreateObjectSpace method to create an Object Space.
在XAF中,您使用对象空间来查询和更新持久对象。调用静态XafApplication. CreateObjectSpace方法来创建对象空间。
Use the IObjectSpace.FirstOrDefault method to find a DemoTask object. This method has the following parameter:
使用IObjectSpace. FirstOrDefault方法查找DemoTask对象。此方法具有以下参数:
- A lambda expression to search for an object.(用于搜索对象的lambda表达式。)
Create a New View(创建新视图)
To show the found object in a separate Detail View:
要在单独的详细信息视图中显示找到的对象:
- Call the XafApplication.CreateDetailView method to create a View.(调用XafApplication. CreateDetailView方法来创建视图。)
- Assign the View to the e.ShowViewParameters.CreatedView property of the event parameter.(将视图分配给事件参数的e. ShowViewParameters.CreatedView属性。)
Tip
You can initialize the ShowViewParameters property in the Execute event handler of any Action of any type. This allows you to always show a View after an Action is executed.
您可以在任何类型的任何Action的Execute事件处理程序中初始化ShowViewParameter属性。这允许您在Action执行后始终显示View。
For more information on how to show a View in a separate window, refer to the following topic: Ways to Show a View.
有关如何在单独窗口中显示视图的详细信息,请参阅以下主题:显示视图的方法。
Manage a Cross-Platform .NET App UI Application(管理跨平台.NET应用程序UI应用程序)
Use XafApplication object when you need to create a List View, Detail View, Object Space, etc. You can access it from various locations in an XAF application. For example, to access this object from the controller, use the property.
当您需要创建列表视图、详细信息视图、对象空间等时,请使用XafApplication对象。您可以从XAF应用程序中的不同位置访问它。例如,要从控制器访问此对象,请使用属性。
Note
CodeRush allows you to add Actions and Controllers with a few keystrokes. To learn about the Code Templates for XAF, refer to the following help topic: XAF Templates.
CodeRush允许您通过几次击键添加操作和控制器。要了解XAF的代码模板,请参阅以下帮助主题:XAF模板。