Avalonia Menu Factory is a dynamic UI library that lets you seamlessly implement and manage a menu bar in your Avalonia application from only a class, where each function is a menu item.
Avalonia Menu Factory works by reading a decorated class of methods and sending the results into an Menu
control.
Use the static MenuFactory.Generate()
method, passing in an instance of your menu class to create the menu items. These items can then be assigned to the Menu.Items
property.
MenuRoot = this.FindControl<Menu>("MenuRoot")!;
MenuModel menuModel = new();
MenuRoot.Items = MenuFactory.Generate(menuModel);
To build your menu class, decorate each method with the Avalonia.MenuFactory.MenuAttribute
attribute where you can specify the MenuItem
name, navigation path (e.g. About/Help
for About > Help > {method}
), icon, and hotkey.
[Menu("Exit", "_File", Icon = MaterialIconKind.ExitToApp, HotKey = "Alt + F4")]
public static void Exit()
{
Environment.Exit(0);
}
[Menu("Readme", "_About/Help", Icon = MaterialIconKind.HelpOutline)]
public static void Readme()
{
Debug.WriteLine(File.ReadAllText("../ReadMe.md"));
// ...
}
[Menu("Credits", "_About", Icon = MaterialIconKind.PersonCheck)]
public static async void Credits()
{
Debug.WriteLine(File.ReadAllText("../Credits.md"));
// ...
}
The previous two examples will build the following menu:
Install with NuGet or build from source.
Install-Package AvaloniaMenuFactory
git clone https://github.com/ArchLeaders/Avalonia.MenuFactory.git
dotnet build Avalonia.MenuFactory
© 2022 Arch Leaders