dialog - Gtk# Windows 是否有等效的 Form.Showdialog?

标签 dialog gtk#

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

我只是尝试让窗口成为​​模态窗口,但尽管它阻止了用户与调用窗口交互,但它不会在运行 ShowAll() 之后的代码之前等待用户关闭对话框。

最佳答案

不使用 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 ();

请注意,在 GTK# 中 Dispose()ing 一个小部件不会在 GTK# 中 Destroy() 它——一个历史性的设计事故,为向后兼容而保留。但是,如果您使用自定义对话框子类,则可以重写 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 ();

当然,您可以更进一步,编写与 ShowDialog 等效的内容。

关于dialog - Gtk# Windows 是否有等效的 Form.Showdialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/990640/

相关文章:

c++ - 永远在前面的对话框

.net - 如何删除 GTK# 窗口的任务栏条目?

c# - 如何在单声道(GTK)中更改窗口背景颜色?

c# - Monodevelop 和 Gtk Treeview

jQuery:IE6 中的窗口控件问题

android - 我想制作一个像 WhatsApp 个人资料照片对话框屏幕这样的对话框屏幕

java - eclipse 中的 MessageDialog 不工作

android - 他们是怎么做到的呢?主屏幕上的对话框

.net - GTK+ 和 GTK# 有什么区别,哪个是 "more multiplatform"?

c# - StatusIcon 未出现在通知区域