captain_whiskey
captain_whiskey

Reputation: 63

Instantiate an additional window with monomac/c#

I've been searching all over for the answer to this question, and while I realize it's likely very trivial, somehow the answer eludes me.

I need to show a second window (launched from clicking a menu item, if that matters). I know perfectly well how to do this with winforms, but I'm not sure what the monomac/NSWindow equivalent is. I need to do this:

MyWindowClass myWindow = new MyWindowClass();
myWindow.Show();

The best info I can find on the subject says that the following should work:

MyWindowClass myWindow = new MyWindowClass();
myWindow.MakeKeyAndOrderFront(this);

But when I try that, it tells me that MyWindowClass() needs an overload, so I look at the constructor and see that it's asking for an IntPtr. Not knowing any better, I try this:

MyWindowClass myWindow = new MyWindowClass(new IntPtr());
myWindow.MakeKeyAndOrderFront(this);

This code will run without error, yet nothing happens when I try to launch the window.

I'm sure I'm just making a silly mistake, but I just haven't been able to find anything on the subject. Any help would be greatly appreciated.

Edit: For reference, here's the relevant portion of the constructor:

public MyWindowClass (IntPtr handle) : base (handle)
    {
        Initialize ();
    }

Additional info: I'm trying to run the code above from within AppDelegate.cs in the following method:

partial void showWindow (MonoMac.Foundation.NSObject sender){
        MyWindowClass myWindow = new MyWindowClass(new IntPtr());
        myWindow.MakeKeyAndOrderFront(this);
    }

EVEN MORE CODE AHOY:

public partial class ViewPaths : MonoMac.AppKit.NSWindow
{
    #region Constructors
    public ViewPaths ()
    {
        Initialize();
    }

    // Called when created from unmanaged code
    public ViewPaths (IntPtr handle) : base (handle)
    {
        Initialize ();
    }

    // Called when created directly from a XIB file
    [Export ("initWithCoder:")]
    public ViewPaths (NSCoder coder) : base (coder)
    {
        Initialize ();
    }

    // Shared initialization code
    void Initialize ()
    {
    }

    #endregion
}

And then the actual instantiation:

public partial class AppDelegate : NSApplicationDelegate
{
    MainWindowController mainWindowController;
    ViewPaths display;
    public AppDelegate ()
    {
    }

    public override void FinishedLaunching (NSObject notification)
    {
        mainWindowController = new MainWindowController ();
        mainWindowController.Window.MakeKeyAndOrderFront (this);
    }


    partial void viewPaths (MonoMac.Foundation.NSObject sender){
        display = new ViewPaths();
        display.MakeKeyAndOrderFront(this);
    }

}

}

This shows a window with no UI elements of any kind.

Upvotes: 3

Views: 497

Answers (2)

captain_whiskey
captain_whiskey

Reputation: 63

For clarity, here's the final code that solved the problem, should anyone else ever google this:

Since I had created a new monomac window with a controller, I needed to create an instance of that controller, and show the controller's window:

MyWindowController myWindow = new MyWindowController();
myWindow.Window.MakeKeyAndOrderFront(this);

This did not need a new constructor without a handle parameter implemented - that solution was working around the problem I'd created by instantiating the incorrect thing.

Upvotes: 2

Curtis
Curtis

Reputation: 1602

Just add a default constructor without the handle parameter. Make sure MyWindowClass subclasses NSWindow and it should work.

Also, you may need to keep a reference to your myWindow around - so that it does not get garbage collected.

Upvotes: 2

Related Questions