I am using Blazor Server. Inside my startup I am configuring the MenuItems like this:
var menu = new[]
{
new MenuItem
{
Label = "File", Submenu = new[]
{
new MenuItem
{
Label = "Save",
Accelerator = "CmdOrCtrl+S",
Enabled = false,
Click = () =>
{
// How do I execute code inside my component?
},
}
}
}
};
Electron.Menu.SetApplicationMenu(menu);
My component lives on the root and is always rendered. It has a simple method called Save
which I want to call.
public async Task SaveProject()
{
await Project.Save();
}
Isn't there some kind of event inside the static Electron class that I could use? Something like OnMenuItemClicked
.
Having a static property that I could access inside my component would not only be bad design, it would prevent me from accessing any instance properties.
I came up with a more or less applicable solution myself. I have created a singleton service IMenuItemService which I am using in both my Startup and my Component. Since MenuItems do not have an ID per se, I created a seperate Enum
MenuItemType
to seperate them. The service looks something like this:With this approach I can call
menuItemService.RenderMenuItems()
from anywhere in my application includingStartup.cs
while in my Components I am setting theMenuItemClicked
Action in order to listen to clicks.In my case I intentionally used an Action Property instead on an EventHandler since I do not need multiple listeners for my MenuItem.