Ways to Register a Module(注册模块的方法 )
In an XAF application, you can use modules declared in the current solution and modules provided by external assemblies. This topic lists all available techniques to register a module in your applications.
在XAF应用程序中,您可以使用当前解决方案中声明的模块和外部程序集提供的模块。本主题列出了在应用程序中注册模块的所有可用技术。
Use the Solution Wizard(使用解决方案向导)
You can add modules to your application when you use the Solution Wizard to create a new XAF solution. Select modules in the Choose Additional Modules step.
当您使用解决方案向导创建新的XAF解决方案时,您可以将模块添加到您的应用程序中。在选择其他模块步骤中选择模块。
Note
In the Solution Wizard, you can only add XAF modules from the predefined list. To add a custom module, use other techniques described in this topic.
在解决方案向导中,您只能从预定义列表中添加XAF模块。要添加自定义模块,请使用本主题中描述的其他技术。
Add a Module in Code(在代码中添加模块)
This section lists several techniques you can use to add a module to your .NET and .NET Framework projects in code.
本节列出了几种可用于将模块添加到. NET和.NET Framework项目中的代码。
Your application must execute the code that registers a module before it calls the XafApplication.Setup method. In XAF, you cannot add a module after the Setup method call.
您的应用程序必须在调用XafApplication. Setup方法之前执行注册模块的代码。在XAF中,您不能在Setup方法调用之后添加模块。
Register a Module in the Application Project(在应用程序项目中注册一个模块)
In the application project, you can use the application builder to register an additional module:
在应用程序项目中,您可以使用应用程序构建器注册一个附加模块:
1.In a .NET project, install the NuGet package with the additional module. In a .NET Framework project, add the reference to the additional module assembly instead.
在一个.NET项目,安装带有附加模块的NuGet包。在. NET Framework项目中,改为添加对附加模块程序集的引用。
2.Navigate to the MyApplication.Blazor.Server\Startup.cs file (ASP.NET Core Blazor) or the MyApplication.Win\Startup.cs file (Windows Forms) and add the module to your application with the help of the application builder.
导航到MyApplication. Blazor.Server\Startup.cs文件(ASP.NETCore Blazor)或MyApplication.Win\Startup.cs文件(Windows窗体),并在应用程序构建器的帮助下将模块添加到应用程序中。
ASP.NET Core Blazor
public class Startup {
// ...
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddXaf(Configuration, builder => {
// ...
builder.Modules
.AddReports(/*...*/)
.AddDashboards(/*...*/)
.Add<MyCustomModule>();
// ...
});
// ...
}
// ...
}
Windows Forms
//...
public class ApplicationBuilder : IDesignTimeApplicationFactory {
public static WinApplication BuildApplication(string connectionString) {
// ...
builder.Modules
.AddReports(/*...*/)
.AddDashboards(/*...*/)
.Add<MyCustomModule>();
// ...
}
// ...
}
If your application does not have an application builder, add an instance of the module to the XafApplication.Modules list in the WinApplication/WebApplication descendant’s constructor:
如果您的应用程序没有应用程序构建器,请将模块的实例添加到WinApplication/WebApplication后代构造函数中的XafApplication. Modules列表中:
C#
public MySolutionWinApplication() {
InitializeComponent();
this.Modules.Add(new MyCustomModule.CustomModule());
}
This technique may require additional setup for certain modules. You can find relevant information in module-specific help topics.
此技术可能需要对某些模块进行额外设置。您可以在特定于模块的帮助主题中找到相关信息。
Register a Module in the Module Project(在模块项目中注册一个模块)
In an existing module project, you can register an additional module to be loaded with the current module.
在现有模块项目中,您可以注册要与当前模块一起加载的附加模块。
1.In a .NET project, install the NuGet package with the additional module. In a .NET Framework project, add the reference to the additional module assembly instead.
在一个.NET项目,安装带有附加模块的NuGet包。在. NET Framework项目中,改为添加对附加模块程序集的引用。
2.In the module project’s constructor declared in the Module.cs (Module.vb) file by default, add the additional module to the ModuleBase.RequiredModuleTypes list.
在默认情况下在Module. cs(Module.vb)文件中声明的模块项目的构造函数中,将附加模块添加到ModuleBase.必需ModuleTypes列表中。
C#
public sealed class MySolutionModule : ModuleBase {
//...
public MySolutionModule() {
InitializeComponent();
this.RequiredModuleTypes.Add(typeof(MyCustomModule.CustomModule));
}
}
With this technique, some modules may require additional setup. You can find the relevant information in module-specific help topics.
使用这种技术,某些模块可能需要额外的设置。您可以在特定于模块的帮助主题中找到相关信息。
Register a Dynamically Loaded Module(注册动态加载模块)
You can register additional modules that XAF loads dynamically by their names when required.
您可以在需要时通过名称注册XAF动态加载的其他模块。
To register a dynamically loaded module, pass the module’s name to the XafApplication.Setup method. The Setup method has overloads that take the moduleAssemblies parameter - a string array that specifies module assembly names to be loaded.
要注册动态加载的模块,请将模块名称传递给XafApplication. Setup方法。Setup方法具有采用moduleAssemblie参数的重载-一个指定要加载的模块程序集名称的字符串数组。
For instance, you can use this method to load modules listed in the application configuration file. For an example, refer to the Add a Module to the Application Configuration File section of this topic.
例如,您可以使用此方法加载应用程序配置文件中列出的模块。例如,请参阅本主题的将模块添加到应用程序配置文件部分。
This technique is not suitable for .NET XAF applications. To add a module to a .NET XAF application dynamically, navigate to the MyApplication.Blazor.Server\Startup.cs file (ASP.NET Core Blazor) or the MyApplication.Win\Startup.cs file (Windows Forms) and use the Add<TModule>(Func<TModule>) method:
此技术不适用于. NET XAF应用程序。要将模块动态添加到.NET XAF应用程序,请导航到MyApplication.Blazor.Server\Startup.cs文件(ASP.NETCore Blazor)或MyApplication.Win\Startup.cs文件(Windows窗体)并使用Add(Func<TModule>)方法:
cs
IEnumerable<Type> modulesTypesToAdd = /* ... */
foreach (Type moduleType in moduleTypesToAdd) {
builder.Modules.Add(() => (DevExpress.ExpressApp.ModuleBase)Activator.CreateInstance(moduleType));
}
Add a Module to the Application Configuration File(将模块添加到应用程序配置文件)
You can use configuration files in this manner only in .NET Framework applications. In .NET application, use the Add<TModule>(Func<TModule>) method as described above.
您只能在. NET Framework应用程序中以这种方式使用配置文件。在.NET应用程序中,使用如上所述的Add<TModule>(Func<TModule>)方法。
Tip
If you use this technique, third parties may plug in their own modules without recompiling your application.
如果您使用这种技术,第三方可能会插入他们自己的模块,而无需重新编译您的应用程序。
Third-party modules may contain insecure code that overrides security restrictions or modifies your business logic. For this reason, the functionality that allows you to add modules from the configuration file is disabled by default. Enable it in trusted environments only.
第三方模块可能包含覆盖安全限制或修改业务逻辑的不安全代码。因此,默认情况下禁用允许您从配置文件添加模块的功能。仅在受信任的环境中启用它。
1.In a Windows Forms application, edit the Program.cs (Program.vb) file.
在Windows Forms应用程序中,编辑Program. cs(Program.vb)文件。
C#
static class Program {
static void Main() {
//...
MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication();
//...
winApplication.Setup("MySolution", winApplication.ConnectionString,
ConfigurationManager.AppSettings["Modules"].Split(';'));
winApplication.Start();
//...
}
//...
}
In an ASP.NET Web Forms application, edit the Global.asax.cs (Global.asax.vb) file.
在ASP.NET Web Forms应用程序中,编辑Global. asax.cs(Global.asax.vb)文件。
C#
public class Global : System.Web.HttpApplication {
protected void Session_Start(object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionWebApplication());
//...
WebApplication.Instance.Setup("MySolution", WebApplication.Instance.ConnectionString,
ConfigurationManager.AppSettings["Modules"].Split(';'));
WebApplication.Instance.Start();
}
//...
}
2.Add the module assembly name to the modules list in the application configuration file (App.config for Windows Forms or Web.config for ASP.NET Web Forms).
将模块程序集名称添加到应用程序配置文件中的模块列表中(Windows Forms的App. config或ASP.NET Web Forms的Web.config)。
xml
<configuration>
<appSettings>
<add key="Modules" value="MySolution.MyCustomModule" />
</appSettings>
</configuration>
Here, MySolution.MyCustomModule is a custom module assembly name.
在这里,MySolutions. MyCustomModule是一个自定义模块程序集名称。
Note
If the module includes the Entity Framework Core’s DbContext, register its EFCoreObjectSpace provider at the module level. Then, override the ModuleBase.Setup method and handle the XafApplication.CreateCustomObjectSpaceProvider event.
如果模块包含Entity Framework Core的DbContext,请在模块级别注册其EFCoreObjectSpace提供程序。然后,覆盖ModuleBase. Setup方法并处理XafApplication。CreateCustomObjectSpaceProvider事件。
Use the Module Designer or Application Designer (.NET Framework)(使用模块设计器或应用程序设计器(. NET Framework))
In an existing XAF solution, invoke the Application Designer or Module Designer. Drag the module from the Toolbox to the Designer’s Required Modules / Modules section.
在现有的XAF解决方案中,调用应用程序设计器或模块设计器。将模块从工具箱拖到设计器所需的模块/模块部分。
The modules shipped with XAF are available in the DX.24.1: XAF Modules tab of the Toolbox.
XAF附带的模块在工具箱的DX.24.1: XAF模块选项卡中可用。
You can register a custom module declared in an external library in the Toolbox. Drag a registered module to the Application or Module Designer. For details, refer to the following topic: How to: Add Items to the Toolbox.
您可以在工具箱中注册在外部库中声明的自定义模块。将已注册的模块拖到应用程序或模块设计器。有关详细信息,请参阅以下主题:如何:将项目添加到工具箱。