Create a Menu Extension Using C#
Introduction
This how-to describes how to create an extension that adds an item to Studio Pro menu from scratch.
You can download the example in this how-to in this GitHub repository.
Creating an Extension Project
-
Open Visual Studio and create a new project using the
C# Class Librarytemplate. -
Name your project. It recommended to use a format similar to
MyCompany.MyProject.MendixExtension. -
Select the
.NET 8.0Framework. -
Add
Mendix.StudioPro.ExtensionsAPINuGet package to the project references. Pick the version that does not exceed the Studio Pro version you installed. To do so, perform the following steps:-
Include a reference to the Extensions API NuGet package:
-
Add new file named
manifest.jsonto your project. -
Add the following code into the file:
{ "mx_extensions": [ "<name_of_your_project>.dll" ] } -
For the
manifest.jsonfile, right-click Solution Explorer > Properties and change the Copy to Output Directory property to Copy always.
-
Creating a Test Mendix App
Test your extension by creating or using a Mendix app.
- Create a new Mendix app using a starter template, or use an existing app.
- In Studio Pro, go to App > Show App Directory in Explorer to open the app directory.
- Inside the app directory, create a new folder named Extensions.
- Inside the Extensions folder, create a sub-folder named after your extension (for example, MyCompany).
- Copy the full path of the sub-folder:
- Press Shift and right-click at the same time
- Select Copy as path.
- Add the
Post-build eventscript below to your extension project:- Go to Build > Events configuration
- Use this command:
xcopy /y /s /i "$(TargetDir)" "<path_to_folder>"
- Build your extension project by pressing Ctrl + Shift + B
- In Studio Pro, click Synchronize App Directory (or press F4) to load the latest version of your extension.
Creating Your First Extension
To add a menu item to Studio Pro, add the following class:
using System.ComponentModel.Composition;
using Mendix.StudioPro.ExtensionsAPI.UI.Menu;
using Mendix.StudioPro.ExtensionsAPI.UI.Services;
namespace MyCompany.MyProject.MendixExtension;
[method: ImportingConstructor]
[Export(typeof(MenuExtension))]
public class MyMenuExtension(IMessageBoxService messageBoxService) : MenuExtension
{
public override IEnumerable<MenuViewModel> GetMenus()
{
yield return new MenuViewModel("Say hello", () => messageBoxService.ShowInformation("Hello World!"));
}
}Build your extension and press F4 in Studio Pro. Menu items are placed under a corresponding menu with the extensions name. For example, if your extension is named My Extension, your menu items will be located under the Extensions > MyCompany sub-menu.
The Extensibility API provides several services you can use and are injected into your extension classes by using the ImportingConstructor attribute.
Subscribing to Extension Events
You can be notified when your extension has been successfully loaded and unloaded from Studio Pro by subscribing to the ExtensionLoaded and ExtensionUnloading events.
using Mendix.StudioPro.ExtensionsAPI.UI.Events;
namespace MyCompany.MyProject.MendixExtension;
[method: ImportingConstructor]
[Export(typeof(MenuExtension))]
public class MyMenuExtension() : MenuExtension
{
public MyMenuExtension()
{
Subscribe<ExtensionLoaded>(onEvent: () => { MyActionOnLoaded() });
Subscribe<ExtensionUnloading>(onEvent: () => { MyActionOnUnloading() });
}
}Debugging Your Extension
-
Ensure the latest version of the extension code is loaded in Studio Pro.
-
Attach the Visual Studio debugger to Studio Pro:
- Go to Debug > Attach to Process dialog box (or press Ctrl + Alt + P).
- Search for and select
studiopro.exe. - Click Attach.
-
Add a breakpoint inside the
Actiondelegate inMyMenuExtension.GetMenus(). -
Trigger the breakpoint by clicking Extensions > MyCompany > Say hello menu item.
Adding a NuGet Dependency
You can access reusable .NET libraries via NuGet Follow the steps below for a one-time setup:
-
In Visual Studio, open your extension project
.csprojfile by right-clicking Solution Explorer > Edit Project File. -
Add the following line inside the first
<PropertyGroup>:<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> -
Use the Manage NuGet Packages to add a dependency.