Home > Mobile >  Xamarin.Forms in new window in Xamarin.Mac project
Xamarin.Forms in new window in Xamarin.Mac project

Time:09-18

I'm trying to embed a XAML page created with Xamarin.Forms in my native Xamarin.Mac application. And open this window as modal. But problem is that opened window ignore size 680х425 and opened as very small square. Where is my problem? Here is my code:

            this.window = new NSWindow(new CGRect(new CGPoint(100, 100), new CGSize(680, 425)), NSWindowStyle.Closable, NSBackingStore.Buffered, false);          

            PickInstallPath pickInstallPath = new();

            this.window.ContentViewController = pickInstallPath.CreateViewController();

            NSApplication.SharedApplication.RunModalForWindow(this.window);

P.S If I remove line where I'm setting ContentViewController window opening in normal size.

CodePudding user response:

The process for consuming a Xamarin.Forms ContentPage-derived page in a native project is as follows:

Add the Xamarin.Forms NuGet package to the native project.

Add the ContentPage-derived page, and any dependencies, to the native project.

Call the Forms.Init method.

Construct an instance of the ContentPage-derived page and convert it to the appropriate native type using one of the following extension methods:

CreateViewController for iOS, CreateSupportFragment for Android, or CreateFrameworkElement for UWP. Navigate to the native type representation of the ContentPage-derived page using the native navigation API.

You can refer to this document to get more information https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/native-forms

Edit: You may want to call SetFrame AFTER you set the ContentViewController as the frame of the view controller's content will override the window's set frame, or you can set the frame of the ViewController's content and not set the window frame at all. You can add code like:

Window.ContentViewController.PreferredContentSize = size;  
  • Related