2010-08-03 129 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