2010-08-03 82 views
1

嗯,我使用一個窗口作爲我的自定義消息框與幾個控件顯示/填充文本取決於哪個構造函數被調用。自定義消息框建議

我有一個定義的事件,它是通過原始類訂閱的,一旦按鈕被點擊就會觸發。

但是我不明白如何有效地使用它,最好是我想返回一個bool,無論是單擊還是否,但顯然我的代碼將繼續執行,因此,該方法是子按鈕到按鈕單擊。以下是一些使問題更清楚的示例代碼。

消息框窗口

public partial class CustomMessageBox : Window 
    { 

     public delegate void MessageBoxHandler(object sender, EventArgs e); 
     public event MessageBoxHandler MessageBoxEvent; 

     public CustomMessageBox() 
     { 
      InitializeComponent(); 
     } 

     public CustomMessageBox(string message) 
     { 
      InitializeComponent(); 
      this.txtdescription.Text = message; 
     } 

     public CustomMessageBox(string message, string title, string firstBtnText) 
     { 
      InitializeComponent(); 
      this.lbltitle.Content = title; 
      this.txtdescription.Text = message; 
      this.btnstart.Content = firstBtnText; 
     } 

    } 

    public static class MessageBoxButtonClick 
    { 

     public static bool Yes { get; set; } 
     public static bool No { get; set; } 
     public static bool Cancel { get; set; } 
    } 

窗口,實例化的MessageBox窗口

private void StartProcess_Click(object sender, System.Windows.RoutedEventArgs e) 
     { 

      foreach (var result in results) 
      { 
       if(result.ToBeProcessed) 
        _validResults.Add(new ToBeProcessed(result.Uri, result.Links)); 

      } 
      _msgbox = new CustomMessageBox("Each Uri's backlinks will now be collected from Yahoo and filtered, finally each link will be visited and parsed. The operation is undertaken in this manner to avoid temporary IP Blocks from Yahoo's servers.", "Just a FYI", "OK"); 
      _msgbox.MessageBoxEvent += (MessageBoxHandler); 

      if (_msgBoxProceed) 
      { 
       _msgbox.Close(); 
       Yahoo yahoo = new Yahoo(); 

       yahoo.Status.Sending += (StatusChange); 

       //What I'd like to happen here is the code simply stop, like it does when calling a messagebox is winforms 
       //e.g. 
       // if(ProceedClicked == true) 
       // do stuff 

       // yahoo.ScrapeYahoo(_validResults[Cycle].Uri, _validResults[Cycle].LinkNumber); 

       //Cycle++; 
      } 
      else 
      { 
       _msgbox.Close(); 
      } 

     } 

private void MessageBoxHandler(object sender, EventArgs e) 
     { 
      if (MessageBoxButtonClick.Yes) 
      { 
       ProceedClicked = true; 
      } 
      else 
      { 
       ProceedClicked = false; 
      } 
     } 

希望這使得它很清楚,我不能把任何執行代碼即調用一定方法,因爲在我的應用程序中多次使用它。

回答

1

很難理解到底是什麼問題。此外,您在這裏編寫的代碼似乎沒有任何調用,實際上會顯示CustomMessageBoxWindow。

但我會採取刺這個... 首先,我正確地猜測,在您的主窗口中,您希望您的代碼在if(_msgBoxProceed)處等待,直到用戶實際按下按鈕你的CustomMessageBoxWindow(目前它只顯示消息框並繼續執行下一個語句)?

如果是這樣,那麼我猜你正在用Show()方法顯示你的消息框窗口。改用ShowDialog()。這將導致代碼執行停止,直到消息框被關閉。

如果您不想使用模態對話框,那麼有兩個選項。可以使用線程同步對象(例如AutoResetEvent),也可以在消息框關閉時設置一個新事件,並在關閉的事件處理程序中繼續執行代碼(在StartProcess_Click中,最後一行是對_msgBox.Show()如果(_msgBoxProceed)將在關閉的事件處理程序中)。

+0

嘿馬克,我沒有意識到Windows中的WPF有ShowDialog屬性,所以我的問題得到解決。 謝謝隊友。 – Ash 2010-08-04 11:52:54