Search code examples
c#macoscocoaxamarinxamarin.mac

Opening a New Custom Window From Other in Xamarin.Mac


I have two customized NSWindow in my application like these:

Window 1:

public partial class LoginVindow : NSWindow
    {
        public LoginVindow (IntPtr handle) : base (handle)
        {
        }
    }

Window 1

Window 2:

public partial class OperationWindow : NSWindow
    {
        public OperationWindow (IntPtr handle) : base (handle)
        {
        }
    }

Window 2

Now, I want close the first Window after a button click and open the second Window. However, this code cannot run for me.

 partial void LoginButton_Clicked(Foundation.NSObject sender)
    {
        Window.Close(); // Closes the first login window.

        var operation_window = new OperationWindow(Handle); // Gets the second Window. IntPtr parameter is required unlike the internet codeblocks.

        operation_window.ShowWindow(this); // No any method like this.
    }

All of the code blocks over internet seems for non-customized standard class NSWindow items but one of them is seems stable for me. However, it is not running. (Post in Xamarin Forums: https://forums.xamarin.com/discussion/122359/open-and-close-window-programmatically-xamarin-mac)

How can I handle this operation ? Please help, thanks.


Solution

  • This code block fixed my problem. Thanks.

        // Get new window
        var storyboard = NSStoryboard.FromName("Main", null);
        var controller = storyboard.InstantiateControllerWithIdentifier("OperationsWindow") as NSWindowController;
        controller.ShowWindow(this);
        this.Window.Close();