3

I've a question about an C# application for MacOS.

I have the following code (in the MainClass):

NSMenuItem copyMenuItem = new NSMenuItem(title: "Copy", charCode: "c", handler: delegate
{
     //Has to be code       
});

And in the page.xaml I have an editor tag:

<Editor x:Name="editField" 
          Text="Some Text" 
          Margin="0" 
          Grid.Row="1" 
          Grid.Column="1"/>

So, my question is:

How can I, whilst they use the same solution, copy the text (to a clipboard) which will be in the editor, using the first part of code? (NSMenuItem).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

Since a macOS Xamarin.Forms' based Application has no main menu (usually provided via a Storyboard / xib), you need to construct the entire menu chain:

var mainMenu = new NSMenu();

var appMenuItem = new NSMenuItem();
mainMenu.AddItem(appMenuItem);

var editMenuItem = new NSMenuItem();
mainMenu.AddItem(editMenuItem);

var editSubMenu = new NSMenu("Edit");
editSubMenu.SetSubmenu(editSubMenu, editMenuItem);

var cutMenuItem = new NSMenuItem("Cut", new Selector("cut:"), "x");
editSubMenu.AddItem(cutMenuItem);

var copyMenuItem = new NSMenuItem("Copy", new Selector("copy:"), "c");
editSubMenu.AddItem(copyMenuItem);

var pasteMenuItem = new NSMenuItem("Paste", new Selector("paste:"), "v");
editSubMenu.AddItem(pasteMenuItem);

NSApplication.SharedApplication.MainMenu = mainMenu;

Place that in your AppDelegate (constructor or DidFinishLaunching) and you will have Cut/Copy/Paste for your entire app as the NSMenuItem items are using the Cocoa selectors for the clipboard.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165