Open and Close Window Programmatically Xamarin.Mac

FotalFotal Member ✭✭✭

Hi.

I think this is a simple question, but I have not found the answer to it.
How can I open other windows of my application from the ViewController code?
How can I close the current window from the ViewController code?

Tagged:

Answers

  • FotalFotal Member ✭✭✭

    Any ideas?

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    If you want to "open" a new Window, you can just instance the associated view controller and call ShowWindow:

        MySecondWindowController controller;
        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();
            controller = new MySecondWindowController ();
            controller.ShowWindow (this);
        }
    

    make sure to keep the controller in scope for as long as you want the window, when it is Disposed it will close the window automatically.

    NSWindowController has a Close method on it and NSWindow has a Close as well.

    If you have not yet review the documentation and samples listed here https://forums.xamarin.com/discussion/comment/306182/#Comment_306182 I would consider doing so.

  • FotalFotal Member ✭✭✭
    edited February 2018

    I can't use this with storyboard, because all my UI is a related ViewControllers, I do not have a NSWindowController. I simple create Segue from other controllers and use PrepareSegue (string segueIdentifier, sender) method.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    edited February 2018

    Then I'm not sure at all what you are asking. You could create a view controller of the view you want in a new window, and root that in a new window or PrepareSegue depending on how you have it setup.

  • YuriKuznetsovYuriKuznetsov USMember ✭✭✭
    edited March 2018

    for close you can do this

    NSApplication.SharedApplication.KeyWindow.Close();
    

    if you want to close window of current ViewController

    View.Window.Close();    
    

    to open new window you can do that

    var window = new NSWindow();
    window.OrderFront(window);
    
  • FotalFotal Member ✭✭✭
    edited March 2018

    Ok @YuriKuznetsov i will try this later, thanks

Sign In or Register to comment.