首页 > 解决方案 > Xamarin Forms Multi-App - MacOS 项目未显示 Xamarin 表单

问题描述

编辑:添加到我创建的项目的链接: https ://drive.google.com/file/d/1A4XGKdxeC22_wqtNEtenLxYbgCp3I_dA/view?usp=sharing

我创建了一个非常基本的多平台应用程序 I Visual Studio 2019 (Windows 10) 有 4 个项目:Source Project Android iOS UWP

我按照以下说明添加了 MacOS 项目:
https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/other/mac

该应用程序编译但加载一个空白表单,并且不会按照 UWP、iOS 和 Android 应用程序显示任何 Xamarin 表单。

接下来,我在 Mac 上本地创建了一个空的 Multi-Platform 应用程序 - 按照上述说明添加 MacOS 并获得相同的结果,即加载空白表单。

我在 Mac 项目中使用的代码是上面的链接。

// AppDelelegate.cs
using AppKit;
using Foundation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
using pbTrader;

namespace pbTrader.MacOS
{
    [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!"; // choose your own Title here
            window.TitleVisibility = NSWindowTitleVisibility.Hidden;
        }

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

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

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


// Main.cs
using AppKit;

namespace pbTrader.MacOS
{
    static class MainClass
    {
        static void Main(string[] args)
        {
            NSApplication.Init();
            NSApplication.SharedApplication.Delegate = new AppDelegate(); // add this line
            NSApplication.Main(args);
        }
    }
}

标签: c#xamarinxamarin.formsmultiplatform

解决方案


推荐阅读