Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeMenu Sample #60

Open
IvanJosipovic opened this issue Sep 8, 2024 · 1 comment
Open

NativeMenu Sample #60

IvanJosipovic opened this issue Sep 8, 2024 · 1 comment

Comments

@IvanJosipovic
Copy link
Sponsor Contributor

IvanJosipovic commented Sep 8, 2024

Hi,

I am struggling to implement a NativeMenu in a declarative Window. Any suggestions?

Update, I managed to get it working with the following:

using AvaloniaEdit.Utils;

namespace App.Views;

public static class MainWindow
{
    public static Window Build(MainViewModel vm) =>
        (Window)new Window()
            .Title("App")
            .UseLayoutRounding(true)
            .SetProp(NativeMenu.MenuProperty, new NativeMenu()
                                                    .Items([
                                                        new NativeMenuItem()
                                                            .Header("File")
                                                            .Items([
                                                                new NativeMenuItem()
                                                                    .Header("Open")
                                                                    .Command(vm.OpenClustersCommand),

                                                                new NativeMenuItemSeparator(),

                                                                new NativeMenuItem()
                                                                    .Header("Close")
                                                                    .Command(vm.CloseCommand),
                                                                ])
            )
            .Content(
				new MainView()
            );
}

public static class WindowExtensions
{
    public static NativeMenu Items(this NativeMenu menu, NativeMenuItemBase[] items)
    {
        menu.Items.AddRange(items);

        return menu;
    }

    public static NativeMenuItem Items(this NativeMenuItem menu, NativeMenuItemBase[] items)
    {
        menu.Menu ??= [];

        menu.Menu.Items.AddRange(items);

        return menu;
    }
}
@AArnott
Copy link
Sponsor

AArnott commented Sep 11, 2024

I got it to work declaratively by adding this under the <Window> element:

  <NativeMenu.Menu>
    <NativeMenu>
      <NativeMenuItem Header="{Binding BackCommandCaption}" Command="{Binding NavigateBackCommand}" IsVisible="{Binding IsNavigateBackVisible}" />
      <NativeMenuItem Header="{Binding AppMenuCaption}">
        <NativeMenu>
          <NativeMenuItem Header="{Binding HomeCommandCaption}" Command="{Binding HomeCommand}" />
          <NativeMenuItem Header="{Binding AddressBookCommandCaption}" Command="{Binding AddressBookCommand}" />
          <NativeMenuItem Header="{Binding AccountsListCommandCaption}" Command="{Binding AccountsListCommand}" />
          <NativeMenuItemSeparator />
          <NativeMenuItem Header="{Binding SettingsCommandCaption}" Command="{Binding SettingsCommand}" />
          <NativeMenuItem Header="{Binding BackupCommandCaption}" Command="{Binding BackupCommand}" />
          <NativeMenuItemSeparator />
          <NativeMenuItem Header="{Binding AboutCommandCaption}" Command="{Binding AboutCommand}" />
        </NativeMenu>
      </NativeMenuItem>

      <NativeMenuItem Header="{Binding ToolsMenuCaption}">
        <NativeMenu>
          <NativeMenuItem Header="{Binding AddressCheckCommandCaption}" Command="{Binding AddressCheckCommand}" />
        </NativeMenu>
      </NativeMenuItem>
    </NativeMenu>
  </NativeMenu.Menu>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants