Skip to main content
All docs
V25.1
  • Create and Register a Custom XAF Module

    • 2 minutes to read

    XAF allows you to implement custom modules in your application and to reuse these modules in other XAF applications.

    Create a Custom XAF Module

    An XAF module is a class library that contains a class derived from ModuleBase. Review the following two options used to create a custom module:

    1. Define a ModuleBase class descendant to convert an existing Class Library into a module. Take the Module.cs file from an existing module as a prototype. Rename this class and set the correct namespace.
    2. Use the Template Kit to add a new XAF Blazor or WinForms project to your application. The new project contains two modules: a platform-specific application project and a common module. Remove the platform-specific project and use the module.

    Important

    Do not inherit from modules. ModuleBase class descendants should be sealed.

    Register a Custom XAF Module in an Existing .NET Application

    1. Add a reference to your module project. To include a module in your application, follow the steps below:

      • Right-click on the Dependencies node of the project where you want to add the module.
      • Select Add Project Reference.
      • In the opened dialog, select your module project and click OK.
    2. Navigate to the MyApplication.Blazor.Server\Startup.cs (Blazor) or MyApplication.Win\Startup.cs (WinForms) file and call the Add method to register the module.

      public class Startup {
         // ...
         public void ConfigureServices(IServiceCollection services) {
               // ...
               services.AddXaf(Configuration, builder => {
                  // ...
                  builder.Modules
                     .Add<CustomModuleProject.CustomModule>();
                  // ...
               });
               // ...
         }
         // ...
      }
      

    Register a Module in the Module Project

    You can use the ModuleBase.RequiredModuleTypes property to specify required dependencies for your module. XAF loads these dependent modules with the current module. Follow the steps below to register custom modules in the Module project:

    1. Add a reference to your module project. To include a module in your application, follow the steps below:
      • Right-click on the Dependencies node of the project where you want to add the module.
      • Select Add Project Reference.
      • In the opened dialog, select your module project and click OK.
    2. In the MyApplication.Module\Module.cs file, add the custom module to the RequiredModuleTypes list.
    public sealed class MyApplicationModule : ModuleBase {
       //...
       public MyApplicationModule() {
          InitializeComponent();
          this.RequiredModuleTypes.Add(typeof(CustomModuleProject.CustomModule));
          // ...
       }
    }
    
    See Also