Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to call another window by code ?

GT4485GT4485 Member ✭✭

Hello.
Mac OS C#, Cocoa application, XIBless.
The Xamarin.Mac - MacDialog sample is very interesting, but it uses a single window. I would like to call another window (as I can call the OpenfileDialog for example), but I do not find any help on the Net. Can anyone give me a link or a code sample ?
Thank you,
Gilbert

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you want to display a new window on the current view controller here is the code:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Do any additional setup after loading the view.
        NSButton btn = new NSButton(new CGRect(0, 100, 100, 44));
        btn.Title = “click”;
        btn.Activated += Btn_Activated;
        View.AddSubview(btn);
    }
    private void Btn_Activated(object sender, EventArgs e)
    {
        NSWindow window = new NSWindow(new CGRect(100, 100, 500, 500), NSWindowStyle.Closable, NSBackingStore.Buffered,false);
        window.ContentViewController = Storyboard.InstantiateControllerWithIdentifier(“SecondVC”) as SecondVC;
        NSWindowController secondWindow = new NSWindowController(window);
        secondWindow.ShowWindow(this);
    }
    
  • GT4485GT4485 Member ✭✭

    Hi LandLu. Thank you for your answer. However, some remarks.
    1) What is "SecondVC" ? I tried to create a new ViewController with this name but it does not work: the statement Storyboard.InstantiateControllerWithIdentifier... works indefinitely.
    2) I saw in Apple doc that Storyboard.InstantiateControllerWithIdentifier uses a "unique identifier for the controller, which you have specified using the Identity inspector in Interface Builder". But I have no Storyboard. This is just the specificity of my question. No storyboard. No XIB. No XCode.
    3) What I want to do is to write a modal dialog (like OpenfileDialog for example), that is called with parameters, displays a modal window, and comes back to the caller with a response.
    Gilbert

  • LandLuLandLu Member, Xamarin Team Xamurai

    Here is a simple view controller:

    private void Btn_Activated(object sender, EventArgs e)
    {
        NSWindow window = new NSWindow(new CGRect(100, 100, 500, 500), NSWindowStyle.Closable, 
        NSBackingStore.Buffered, false);
        var viewController = new MyViewController();
        viewController.View.Frame = window.Frame;
        window.ContentViewController = viewController;
        NSWindowController secondWindow = new NSWindowController(window);
        secondWindow.ShowWindow(this);
    }       
    

    The initialization for MyViewController:

    public class MyViewController : NSViewController
    {
        public MyViewController()
        {
    
        }
        public override void LoadView()
        {
            View = new NSView();
        }
    }
    
  • GT4485GT4485 Member ✭✭

    Hi LandLu. Thanks for your explanation. I am now able to open a second window with the following code.
    However, 1) the second window is not modal, 2) the caller is not notified of the availability of the answer.
    Remember, I just want to call my second window as I can call NSOpenPanel, like that:

    var dlg = NSOpenPanel.OpenPanel;
    dlg.CanChooseFiles = true;
    dlg.CanChooseDirectories = false;
    dlg.AllowedFileTypes = new string[] { "txt", "html", "md", "css" };
    
    if (dlg.RunModal() == 1)
    {
        var url = dlg.Urls[0];
        // etc.
    }
    

    How ?

    using AppKit;
    using CoreGraphics;
    using Foundation;
    using System;
    namespace TestSecondWindow
    {
        public partial class ViewController : NSViewController
        {
            public ViewController(IntPtr handle) : base(handle) { }
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                // Do any additional setup after loading the view.
                NSButton CallButton = new NSButton { Frame = new CGRect(50, 100, 120, 32), Title = "Call dialog" };
                CallButton.Activated += CallButton_Activated;
                View.AddSubview(CallButton);
            }
            public override NSObject RepresentedObject
            {
                get { return base.RepresentedObject; }
                set { base.RepresentedObject = value; } // Update the view, if already loaded. 
            }
            private void CallButton_Activated(object sender, EventArgs e)
            {
                NSWindow SecondWindow = new NSWindow(new CGRect(100, 100, 500, 200)
                    , NSWindowStyle.Closable | NSWindowStyle.Titled
                    , NSBackingStore.Buffered, false);
                SecondWindow.Title = "Dialog";
                var SecondViewController = new CustomDialogViewController();
                SecondViewController.InputParam = "Input parameter";
                SecondViewController.View.Frame = SecondWindow.Frame;
                SecondWindow.ContentViewController = SecondViewController;
                NSWindowController SecondWindowController = new NSWindowController(SecondWindow);
                SecondWindowController.ShowWindow(this);
            }
        }
        public class CustomDialogViewController : NSViewController
        {
            private NSTextField DisplayTextField, EntryTextField;
            public string InputParam { get; set; }
            public string OutputParam { get; set; }
            public CustomDialogViewController() { }
            public override void LoadView() { View = new NSView(); }
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                DisplayTextField = new NSTextField { Frame = new CGRect(20, 100, 96, 21) };
                DisplayTextField.StringValue = InputParam;
                View.AddSubview(DisplayTextField);
                EntryTextField = new NSTextField { Frame = new CGRect(20, 70, 96, 21) };
                View.AddSubview(EntryTextField);
                //InputParam = EntryTextField.StringValue;
                NSButton BtnOK = new NSButton { Frame = new CGRect(20, 30, 81, 32), Title = "OK" };
                BtnOK.Activated += BtnOK_Activated;
                View.AddSubview(BtnOK);
            }
            private void BtnOK_Activated(object sender, EventArgs e)
            {
                OutputParam = EntryTextField.StringValue;
                this.View.Window.Close();
            }
        }
    }
    
Sign In or Register to comment.