简体   繁体   中英

How to open or create new window with MonoMac/Xamarin?

I have a Xamarin/MonoMac project with 2 windows. The main window opens, and functions like it should. However, I have a button that needs to open a second window (Lets call the first window "Main", and the second window "Second"). Both have their respective XIB files, and controller classes.

The button is set up in MainWindowController, and functions normally such that:

secondButton.Activated += (o, e) => {
     //Do stuff
}

Methods such as Console.WriteLine(); function as expected when the button is clicked.

The second window controller class is set up such that:

public partial class SecondWindowController : MonoMac.AppKit.NSWindowController {
    ...
}

What I need to do is cause secondButton to open an instance of SecondWindow upon being clicked, while passing the active instance of MainWindow to the SecondWindow constructor.

In C#.NET I would just use

SecondWindow sc = new SecondWindow(this);
sc.ShowDialog();

I can get SecondWindow to open, using this

NSWindowController nsc = new NSWindowController("SecondWindow", this);
nsc.ShowWindow(this);

But that causes an error stating

"Unknown window Class SecondWindow in Interface Builder file, creating a generic window instead."

The window shows as it should display, but no functions in SecondWindowController function, even if I run

nsc.AwakeFromNib();

directly after opening the window. No console output, and none of the buttons work.

How might I get the window to open, and function properly? If more information is needed, please let me know what I need to add, so I can get that to you.

Thanks in advanced!

To get the window from the Storyboard you would need to use NSStoryboard.InstantiateControllerWithIdentifier(string id) as NSWindowController

Also you must set id as identifier for the window in the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM