In this article, we'll cover the basics of working with Cocoa menu bars, menus, and menu items in a Xamarin. It is highly suggested that you work through the Hello, Mac article first, specifically the Introduction to Xcode and Interface Builder and Outlets and Actions sections, as it covers key concepts and techniques that we'll be using in this article.
Unlike applications running on the Windows OS where every window can have its own menu bar attached to it, every application running on macOS has a single menu bar that runs along the top of the screen that's used for every window in that application:.
Items on this menu bar are activated or deactivated based on the current context or state of the application and its user interface at any given moment. For example: if the user selects a text field, items on the Edit menu will be come enabled such as Copy and Cut. According to Apple and by default, all macOS applications have a standard set of menus and menu items that appear in the application's menu bar:.
For more information about the application menu bar and standard menus and menu items, please see Apple's Human Interface Guidelines.
Whenever you create a new Xamarin. Mac project, you automatically get a standard, default application menu bar that has the typical items that a macOS application would normally have as discussed in the section above. Your application's default menu bar is defined in the Main. Double-click the Main. From here we can click on items such as the Open menu item in the File menu and edit or adjust its properties in the Attributes Inspector :.
We'll get into adding, editing, and deleting menus and items later in this article. For now we just want to see what menus and menu items are available by default and how they have been automatically exposed to code via a set of predefined outlets and actions for more information see our Outlets and Actions documentation.
For example, if we click on the Connection Inspector for the Open menu item we can see it is automatically wired up to the openDocument: action:. If you select the First Responder in the Interface Hierarchy and scroll down in the Connection Inspector , and you will see the definition of the openDocument: action that the Open menu item is attached to along with several other default actions for the application that are and are not automatically wired up to controls :. Why is this important? In the next section will see how these automatically-defined actions work with other Cocoa user interface elements to automatically enable and disable menu items, as well as, provide built-in functionality for the items.
Later we'll be using these built-in actions to enable and disable items from code and provide our own functionality when they are selected.
If you were the run a newly created Xamarin. Mac application before adding any UI items or code, you'll notice that some items are automatically wired-up and enabled for you with fully functionality automatically built-in , such as the Quit item in the App menu:. While other menu items, such as Cut , Copy , and Paste are not:. Let's stop the application and double-click the Main.
In the Constraint Editor let's pin the text view to the window's edges and set it where it grows and shrinks with the window by clicking all four red I-beams at the top of the editor and clicking the Add 4 Constraints button:. Save your changes to the user interface design and switch back the Visual Studio for Mac to synchronize the changes with your Xamarin.
Mac project. Now start the application, type some text into the text view, select it, and open the Edit menu:. Notice how the Cut , Copy , and Paste items are automatically enabled and fully functional, all without writing a single line of code. What's going on here? Remember the built-in predefine actions that come wired up to the default menu items as presented above , most of the Cocoa user interface elements that are part of macOS have built in hooks to specific actions such as copy:.
So when they are added to a window, active, and selected, the corresponding menu item or items attached to that action are automatically enabled. If the user selects that menu item, the functionality built into the UI element is called and executed, all without developer intervention. By default, every time a user event occurs, NSMenu automatically enables and disables each visible menu and menu item based on the context of the application.
If you choose to use manual menu enabling, none of the menu items, even those controlled by AppKit classes like NSTextView , are updated automatically. You will be responsible for enabling and disabling all items by hand in code. As stated above, for any menu item that is bound to a Window or View Controller Custom Action , you can add the validateMenuItem: action and manually enable or disable menu items. The Tag property has been set in Interface Builder for each menu item:. When this code is run, and no text is selected in the NSTextView , the two wrap menu items are disabled even though they are wired to actions on the view controller :.
If a section of text is selected and the menu reopened, the two wrap menu items will be available:. As we have seen above, just by adding specific Cocoa user interface elements to our UI design such as a text field , several of the default menu items will be enabled and function automatically, without having to write any code. Next let's look at adding our own C code to our Xamarin.
Mac project to enable a menu item and provide functionality when the user selects it. For example, let say we want the user to be able to use the Open item in the File menu to select a folder. Since we want this to be an application-wide function and not limited to a give window or UI element, we're going to add the code to handle this to our application delegate.
In the Solution Pad , double-click the AppDelegate. CS file to open it for editing:. Notice that the Open menu item is now enabled. If we select it, the open dialog will be displayed:. If you'll remember from above, the Open menu item is automatically wired-up to this action by default in Interface Builder:. Next let's look at creating our own menu, menu items, and actions and responding to them in code.
By default, the File menu contains an Open Recent item that keeps track of the last several files that the user has opened with your app. If you are creating a NSDocument based Xamarin. Mac app, this menu will be handled for you automatically. For any other type of Xamarin. Mac app, you will be responsible for managing and responding to this menu item manually. To manually handle the Open Recent menu, you will first need to inform it that a new file has been opened or saved using the following:. Next, you need to override the OpenFile method of the app delegate to open any file that the user selects from the Open Recent menu.
For example:. Return true if the file can be opened, else return false and a built-in warning will be displayed to the user that the file could not be opened. Because the filename and path returned from the Open Recent menu, might include a space, we need to properly escape this character before creating a NSUrl or we will get an error.
After updating to VS Community V the Tools | Android menu items are disabled. Trying to run device manager or SDK manger from. Only then the menu came back. Click on Ok. now your Android SDk manager will be in Enable. I had very strange behavior of my Visual Studio: when I just launch it Android tools Forms · Android · iOS · Mac · Xamarin.
We do that with the following code:. Finally, we create a NSUrl that points to the file and use a helper method in the app delegate to open a new window and load the file into it:. To pull everything together, let's take a look at an example implementation in an AppDelegate.
Based on the requirements of your app, you might not want the user to open the same file in more than one window at the same time. In our example app, if the user chooses a file that is already open either from the Open Recent or Open.. We designed our ViewController class to hold the path to the file in its Path property.
Next, we loop through all currently open windows in the app. If the file is already open in one of the windows, it is brought to the front of all other windows using:. If no match is found, a new window is opened with the file loaded and the file is noted in the Open Recent menu:. Just like the built-in First Responder actions that come pre-wired to standard menu items, you can create new, custom actions and wire them to menu items in Interface Builder.
At the moment I'm at a complete standstill and unable to work. Any help to resolve this would be very gratefully received. Copying and pasting this into the terminal on the Mac also results in an error and it's because of the space between Xcode and 9. There's no point in simply renaming Xcode 9. Further searching in the registry and nothing comes up. I've tried looking in appropriate files and can find nothing.
I've even tried watching decent. I'm not getting anywhere. Would have thought it's fairly easy to locate this information and manually change it. I guess it's encoded and stored in binary form somewhere.