Opening a New Custom Window From Other in Xamarin.Mac

berkbababerkbaba TRMember ✭✭

Hi, I have two customized NSWindow in my application like these:

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

Window 2:

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

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.

Best Answer

  • berkbababerkbaba TR ✭✭
    Accepted Answer

    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();
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
  • berkbababerkbaba TRMember ✭✭
    edited October 2018

    Absolutely yes, @ColeX. It not works. My objects needs IntPtr in constructor and I cannot solve it:

            public partial class LoginWindowController : NSWindowController
            {
                public LoginWindowController (IntPtr handle) : base (handle)
                {
                }
            }
    
        public partial class LoginWindow : NSWindow
            {
                public LoginWindow (IntPtr handle) : base (handle)
                {
    
    
    
                }
    
            }
    
    
        public partial class LoginViewController : NSView
            {
                public LoginViewController (IntPtr handle) : base (handle)
                {
                }
    
                partial void LoginButton_Clicked(Foundation.NSObject sender)
                {
    
                    this.Window.Close();
    
                    OperationWindowController controller = new OperationWindowController(new IntPtr(1));
    
                    controller.ShowWindow(new OperationWindow(new IntPtr(1)));
    
                }
    
            }
    
        public partial class OperationWindowController : NSWindowController
            {
                public OperationWindowController (IntPtr handle) : base (handle)
                {
                }
            }
        public partial class OperationWindow : NSWindow
            {
                public OperationWindow (IntPtr handle) : base (handle)
                {
                }
    
            }
    
  • berkbababerkbaba TRMember ✭✭
    Accepted Answer

    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();
    
Sign In or Register to comment.