2013-08-24 132 views
0

我想用單聲道Gtk#編寫一個簡單的窗體應用程序,但我已經卡在開始我創建一個Dialog窗體繼承自Gtk.Dialog。對話框形式用於收集基本信息並將這些信息作爲對象返回到主窗口或觸發某個事件到主窗口,以便在此情況下可以執行它所做的事情,將數據綁定到TreeView控件(這是另一個故事)。這些是我迄今爲止所嘗試的;Mono Gtk.Dialog返回對象作爲響應

對話框代碼

public partial class MyDialog : Gtk.Dialog 
{ 
    public MyDialog() 
    { 
     this.Build(); 
    } 

    protected void OnButtonOkClicked (object sender, EventArgs e) 
    { 
     int portNumber = 0; 
     iint.TryParse (spnPort.Text, out portNumber); 

     var myObj = new MyObj(); 
     myObj.Username = txtUsername.Text; 
     myObj.Password = txtPassport.Text; 

     // did not work as ParentWindow is a Gdk.Window 
     //(this.ParentWindow as MainWindow).AddObj(myObj); 

     //Also did not work because there is no response related method 
     //or property in the Dialog please read below code block this will make more sense 
     //this.OnResponse(myObj); 
    } 


} 

主窗口的代碼來調用對話框幷

protected void OnAddActionActivated (object sender, EventArgs e) 
{ 
    MyDialog s = new MyDialog(); 
    s.Run(); 
    s.Response += HandleResponse; 
} 

void HandleResponse (object o, ResponseArgs args) 
{ 
    //as this event has args.Args and args.RetVal I thought one would do what I wanted 
    //maybe I am using them all wrong 
} 

我很感激,如果有人能解釋什麼是Gdk.Window是什麼,它正在Gtk控制下進行。

回答

1

只需將要返回的對象存儲在對話框對象中,並使用屬性提供對其的訪問。不要忘記檢查用戶是否按下取消按鈕(如果有),方便地通過檢查返回值Run()

有關示例,請參閱官方文檔中的sample code for the stock FileChooserDialog

相關問題