简体   繁体   English

gtk#窗口线程

[英]gtk# thread for window

I'm building a GUI application with C# and gtk#. 我正在使用C#和gtk#构建一个GUI应用程序。 I've encountered an issue recently and was looking for the best solution to this problem: 我最近遇到了一个问题,一直在寻找解决此问题的最佳方法:

I have a modal window that pops up for the user to enter a number. 我有一个模态窗口,供用户输入数字。 This window is a separate window accessed from my main window and it's set up like this: 此窗口是从我的主窗口访问的单独窗口,其设置如下:

    public class MainWindow()
    {
        public NumberEntry numEntry;

Whenever I need numerical input from the user, I call ShowAll() on the public Window property of NumberEntry like: 每当需要用户输入数字时,我都会在NumberEntry的公共Window属性上调用ShowAll(),例如:

    numEntry.win.ShowAll();

And all of this works fine. 所有这些都很好。 Afterwards, to get the value they entered, I call: 然后,为了获得他们输入的值,我致电:

    int entered = numEntry.valueEntered;

The issue is obviously that code continues executing immediately after the ShowAll() line is finished, and numEntry.valueEntered is always 0. What I'd like to do (and have been trying to do), is to suspend the main thread, and open up the number entry window in a second thread, and join back to the main thread when this is complete. 问题显然是在ShowAll()行结束后代码立即继续执行,并且numEntry.valueEntered始终为0。我想做(并且一直在尝试做的)是挂起主线程,并且在第二个线程中打开数字输入窗口,并在完成后重新加入主线程。 Suspending the main thread seems to prevent GUI changes making the program freeze when I try to open the number entry window. 当我尝试打开数字输入窗口时,挂起主线程似乎可以防止GUI更改使程序冻结。 I'd also like to avoid callback methods if at all possible, seeing as how this would get rather complicated after awhile. 我还想尽可能避免使用回调方法,因为一段时间后它会变得相当复杂。 Any advice? 有什么建议吗? Thanks! 谢谢!

Seems like when GTK window is closed all its child controls are cleared. 好像关闭GTK窗口时,其所有子控件都被清除。 So to get the result from the custom dialog window you may do the following (I am not gtk guru but its works for me): 因此,要从自定义对话框窗口中获取结果,您可以执行以下操作(我不是gtk专家,但对我有用):

1. Create a new dialog window with your controls (I used Xamarin studio). 1.使用控件创建一个新的对话框窗口(我使用Xamarin studio)。 Add result properties, OK and Cancel handlers and override OnDeleteEvent method: 添加结果属性,确定和取消处理程序,并覆盖OnDeleteEvent方法:

public partial class MyDialog : Gtk.Dialog
{

    public string Results {
        get;
        private set;
    }

    public MyDialog ()
    {
        this.Build ();
    }

    protected override bool OnDeleteEvent (Gdk.Event evnt)
    {
        Results = entry2.Text; // if user pressed on X button..
        return base.OnDeleteEvent (evnt);
    }

    protected void OnButtonOkClicked (object sender, EventArgs e)
    {
        Results = entry2.Text;
        Destroy ();
    }

    protected void OnButtonCancelClicked (object sender, EventArgs e)
    {
        Results = string.Empty;
        Destroy ();
    }
}

2. In your main window create a dialog object and attach to its Destroyed event your event handler: 2.在主窗口中,创建一个对话框对象并将事件处理程序附加到其Destroyed事件:

protected void OnButtonClicked (object sender, EventArgs e)
{
    var dialog = new MyDialog ();
    dialog.Destroyed += HandleClose;
}

3. Get the results when dialog is closed: 3.在对话框关闭时获取结果:

void HandleClose (object sender, EventArgs e)
{
    var dialog = sender as MyDialog;
    var textResult = dialog.Results;
}

If you whant you also may specify a dialog result property and etс. 如果您愿意,也可以指定对话框结果属性和etс。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM