Mac 平台設定

開始之前,請先建立 (或使用現有的) Xamarin.Forms 專案。 您只能使用 Visual Studio for Mac 新增 Mac 應用程式。

將macOS專案新增至 Xamarin.Forms 影片

新增 Mac 應用程式

請依照下列指示新增將在 macOS Sierra 和 macOS El Capitan 上執行的 Mac 應用程式:

  1. 在 Visual Studio for Mac 中,以滑鼠右鍵按單擊現有的 Xamarin.Forms 方案,然後選擇 [ 新增 > 新增專案...

  2. 在 [ 新增專案] 視窗中,選擇 [Mac > 應用程式 > Cocoa 應用程式 ],然後按 [下一步]。

  3. 輸入應用程式名稱(並選擇性地為停駐專案選擇不同的名稱),然後按 [下一步]。

  4. 檢閱組態,然後按 [建立]。 下列步驟如下所示:

    Animated instructions showing how to add a Cocoa app

  5. 在 Mac 專案中,以滑鼠右鍵按兩下 [套件 > 新增套件... ] 以新增 Xamarin.Forms NuGet。 您也應該更新其他專案,以使用相同的 NuGet 套件版本 Xamarin.Forms 。

  6. 在 Mac 專案中,以滑鼠右鍵按兩下 [ 參考 ],並新增專案的參考 Xamarin.Forms (共享專案或 .NET Standard 連結庫專案)。

    Add a reference to the Xamarin.Forms shared code project

  7. 更新 Main.cs 以初始化 AppDelegate

    static class MainClass
    {
        static void Main(string[] args)
        {
            NSApplication.Init();
            NSApplication.SharedApplication.Delegate = new AppDelegate(); // add this line
            NSApplication.Main(args);
        }
    }
    
  8. 更新 AppDelegate 以初始化 Xamarin.Forms、建立視窗,並載入 Xamarin.Forms 應用程式(記得設定適當的 Title)。 如果您有其他需要初始化的相依性,也請在這裡執行此動作。

    using Xamarin.Forms;
    using Xamarin.Forms.Platform.MacOS;
    // also add a using for the Xamarin.Forms project, if the namespace is different to this file
    ...
    [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);
        }
    }
    
  9. 按兩下 Main.storyboard 以在 Xcode 中編輯。 選取 [ 視窗 ],然後 取消核取 [ 是初始控制器 ] 複選框(這是因為上述程式代碼會建立視窗):

    Uncheck the Is Initial Controller checkbox in Xcode

    您可以在分鏡文本中編輯功能表系統,以移除不必要的專案。

  10. 最後,從所需的現有平台專案新增任何本機資源(例如圖像檔)。

  11. Mac 項目現在應該會在 macOS 上執行您的程式 Xamarin.Forms 代碼!

後續步驟

設定樣式

透過最近對 OnPlatform 平臺所做的變更,您現在可以以任意數目的平臺為目標。 這包括macOS。

<Button.TextColor>
    <OnPlatform x:TypeArguments="Color">
        <On Platform="iOS" Value="White"/>
        <On Platform="macOS" Value="White"/>
        <On Platform="Android" Value="Black"/>
    </OnPlatform>
</Button.TextColor>

請注意,您也可以在如下的平臺上加倍: <On Platform="iOS, macOS" ...>

視窗大小和位置

您可以在 中 AppDelegate調整視窗的初始大小與位置:

var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);  // x, y, width, height

已知問題

這是預覽版,因此您應該預期並非所有專案都已就緒。 以下是您在將 macOS 新增至專案時可能會遇到的一些情況:

並非所有 NuGet 都已準備好用於 macOS

您可能會發現您使用的某些連結庫尚未支援macOS。 在此情況下,您必須將要求傳送至項目的維護者以新增它。 在他們獲得支援之前,您可能需要尋找替代方案。

遺漏 Xamarin.Forms 功能

Xamarin.Forms並非所有功能都會在此預覽版中完成。 如需詳細資訊,請參閱 GitHub 存放Xamarin.Forms庫中的平臺支援macOS狀態。