首页 > 解决方案 > 在 Xamarin.Mac 中从其他人打开新的自定义窗口

问题描述

我的应用程序中有两个自定义的 NSWindow,如下所示:

窗口 1:

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

窗口 1

窗口 2:

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

窗口 2

现在,我想在单击按钮后关闭第一个窗口并打开第二个窗口。但是,此代码无法为我运行。

 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.
    }

互联网上的所有代码块似乎都适用于非自定义的标准类 NSWindow 项目,但其中一个对我来说似乎是稳定的。但是,它没有运行。(在 Xamarin 论坛中发帖:https ://forums.xamarin.com/discussion/122359/open-and-close-window-programmatically-xamarin-mac )

我该如何处理这个操作?请帮忙,谢谢。

标签: c#macoscocoaxamarinxamarin.mac

解决方案


这个代码块解决了我的问题。谢谢。

    // Get new window
    var storyboard = NSStoryboard.FromName("Main", null);
    var controller = storyboard.InstantiateControllerWithIdentifier("OperationsWindow") as NSWindowController;
    controller.ShowWindow(this);
    this.Window.Close();

推荐阅读