使用 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/