是否有一个Form.Showdialog等同于Gtk#Windows? [英] Is there a Form.Showdialog equivalent for Gtk# Windows?

查看:148
本文介绍了是否有一个Form.Showdialog等同于Gtk#Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Windows窗体或WPF我可以通过调用ShowDialog打开一个对话窗口。我可以使用Gtk#来实现吗?



我尝试只是使窗口模式,但是尽管它阻止用户与调用窗口进行交互,但并不等待用户在ShowAll()之前运行代码之前关闭对话框。

解决方案

而不是使用Gtk.Window,使用 Gtk.Dialog ,然后调用对话框。跑 ()。这将返回与用户用于关闭对话框的按钮ID相对应的整数值。



例如

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

try {
dialog = new Dialog(
Dialog Title,
parentWindow,
DialogFlags.DestroyWithParent | DialogFlags.Modal,
覆盖文件,ResponseType.Yes,
取消,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()处理一个小部件不会在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();

当然,你可以再进一步写一个等价的ShowDialog。


Using Windows Forms or WPF I can open a dialog window by calling ShowDialog. How can I do that using Gtk#?

I tried just making the Window modal, but although it prevents the user from interacting with the calling window it does not wait for the user to close the dialog before running the code after ShowAll().

解决方案

Instead of using a Gtk.Window, use Gtk.Dialog, then call dialog.Run (). This returns an integer value corresponding to the ID of the button the user used to close the dialog.

e.g.

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 ();

Note that Dispose()ing a widget in GTK# doesn't Destroy() it in GTK# -- a historical design accident, preserved for backwards-compatibility. However, if you use a custom dialog subclass you can override Dispose to also Destroy the dialog. If you also add the child widgets and the ShowAll() call in the constructor, you can write nicer code like this:

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

if (response == ResponseType.Yes)
        OverwriteFile ();

Of course, you could take it a step further and write an equivalent of ShowDialog.

这篇关于是否有一个Form.Showdialog等同于Gtk#Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆