Search code examples
c#macosxamarin.forms.net-standard

Xamarin Forms MacOS and NetStandard


I've just started looking at this and have run into an issue that is preventing me from running my MacOS application. I've followed all the steps detailed in this blog post:

https://blog.xamarin.com/preview-bringing-macos-to-xamarin-forms/

The Xamarin Forms project where the XAML resides has been changed from a PCL to a NetStandard project using these instructions:

https://blog.xamarin.com/building-xamarin-forms-apps-net-standard/

But for some reason it claims it cannot find my App class. Despite it being found at design time and coloured as if it is a type and it even shows up in intellisense as a class. When I compile the project it errors with:

MacOS/AppDelegate.cs(40,40): Error CS0234: The type or namespace name 'App' does not exist in the namespace 'Oracle' (are you missing an assembly reference?) (CS0234) (Oracle.MacOS)

Definitely not missing as assembly reference as it's in intellisense. Anyone else seen this? What is the problem? Is there a way to fix this?

App.Xaml.cs

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Oracle
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            SetMainPage();
        }

        public static void SetMainPage()
        {
            Current.MainPage = new TabbedPage
            {
                Children =
                {
                    new NavigationPage(new ItemsPage())
                    {
                        Title = "Browse",
                        Icon = Device.OnPlatform<string>("tab_feed.png",null,null)
                    },
                    new NavigationPage(new AboutPage())
                    {
                        Title = "About Test",
                        Icon = Device.OnPlatform<string>("tab_about.png",null,null)
                    },
                }
            };
        }
    }
}

AppDelegate.cs

[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate
{
    NSWindow _window;
    public AppDelegate()
    {
        var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

        var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
        _window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
        _window.Title = "Xamarin.Forms on Mac!";
        _window.TitleVisibility = NSWindowTitleVisibility.Hidden;
    }

    public override NSWindow MainWindow
    {
        get { return _window; }
    }

    public override void DidFinishLaunching(NSNotification notification)
    {
        Forms.Init();
        LoadApplication(new Oracle.App());
        base.DidFinishLaunching(notification);
    }

    public override void WillTerminate(NSNotification notification)
    {
        // Insert code here to tear down your application
    }
}

UPDATE After some tinkering around I have solved this first issue and now I'm seeing

Error MM0023: Application name 'Oracle.exe' conflicts with another user assembly. (MM0023) (Oracle.MacOS)


Solution

  • 3 Years later.. but today I tried to create my first xamarin forms app for mac and had the same error. To reproduce this error you need 2 projects like "MyApp" (net standard) and "MyApp.Mac" (Xamarin.Mac).

    To fix it you need:

    1. Rename shared project from "MyApp" to "MyApp.Shared".
    2. Check the property of shared project. The name and default namespace should by "MyApp.Shared".
    3. Remove link from mac project to shared project.
    4. Add link again to shared project.