为 MacOS (C#) 添加和打开 DialogPage(或 window)

Adding and Opening an DialogPage (or window) for MacOS (C#)

我是 C# 和 Xamarin 编程的新手,现在我被困住了。

我在一些帮助下制作了一个菜单,现在我可以做 Cut/Copy/Paste 它工作正常,但现在我想为某些用户设置创建一个对话框或 Window 每当他们点击设置时菜单。 (我在 AppDelegate.cs 中制作了菜单)

我在 Google 和 Xamarin 论坛上看了很多,但他们谈论的只是添加一种新的情节提要。我想这会起作用,但我没有使用任何故事板(只是因为我们想用代码制作所有东西)。

在 Xamarin 上发现: https://developer.xamarin.com/guides/mac/user-interface/working-with-dialogs/ 然后是首选项 window(我更喜欢)。

我在 AppDelegate.cs 中编写的代码(必须打开 Window 或对话框的菜单按钮):

var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
    {
        var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

        var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
        prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
        prefWindow.Title = "Settings";
        prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;
    });

'Settings' 顺便说一句,菜单中的按钮可见。

可能我忘记了什么,它打不开

我怎样才能使这个工作?

也许这对你有用。

var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
{
    var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

    var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
    prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
    prefWindow.Title = "Settings";
    prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;

   NSApplication.SharedApplication.RunModalForWindow(prefWindow);

});

我在您的代码中添加了 NSApplication.SharedApplication.RunModalForWindow(prefWindow);。这将打开一个新的 window。

发现于:https://forums.xamarin.com/discussion/22729/xamarin-mac-how-to-set-position-of-modal-window