对于Gtk #Windows,是否有Form.Showdialog等价物?

wei*_*ure 2 dialog gtk#

使用Windows窗体或WPF我可以通过调用ShowDialog打开一个对话窗口.我怎么能用Gtk#做到这一点?

我尝试制作Window模式,但是虽然它阻止用户与调用窗口进行交互,但它不会等待用户在ShowAll()之后运行代码之前关闭对话框.

Mik*_*son 10

而不是使用Gtk.Window,使用Gtk.Dialog,然后调用dialog.Run().这将返回一个整数值,该值对应于用户用于关闭对话框的按钮的ID.

例如

Dialog dialog = null;
ResponseType response = ResponseType.None;

try {
    dialog = new Dialog (
        "Dialog Title",
        parentWindow,
        DialogFlags.DestroyWithParent | DialogFlags.Modal,
        "Overwrite file", ResponseType.Yes,
       "Cancel", ResponseType.No
    );
    dialog.VBox.Add (new Label ("Dialog contents"));
    dialog.ShowAll ();

    response = (ResponseType) dialog.Run ();
} finally {
    if (dialog != null)
        dialog.Destroy ();
}

if (response == ResponseType.Yes)
    OverwriteFile ();
Run Code Online (Sandbox Code Playgroud)

请注意,在GTK#中使用Dispose()移动窗口小部件并不会在GTK#中销毁()它 - 这是一个历史设计事故,为了向后兼容而保留.但是,如果使用自定义对话框子类,则可以将Dispose重写为"销毁"对话框.如果您还在构造函数中添加子窗口小部件和ShowAll()调用,则可以编写更好的代码,如下所示:

ResponseType response = ResponseType.None;
using (var dlg = new YesNoDialog ("Title", "Question", "Yes Button", "No Button"))
    response = (ResponseType) dialog.Run ();

if (response == ResponseType.Yes)
        OverwriteFile ();
Run Code Online (Sandbox Code Playgroud)

当然,你可以更进一步,写一个相当于ShowDialog.