【问题标题】:How to stop function once message box displayed显示消息框后如何停止功能
【发布时间】:2012-10-05 10:18:55
【问题描述】:
public void CreateFileOutput(object parameter)
{
    TransactFileCreation();

    WPFMessageBox.Show("test", "Process completed successfully.");
}

public void TransactFileCreation()
{
    if (BatchFolderPath == null)
    {
         WPFMessageBox.Show("test", "Select a Batch folder");
         return;
    }
    // code..
}

我从 CreateFileOutput() 调用 TransactFileCreation()。一旦显示消息框,该功能将不再起作用。但就我而言,它再次转到主功能并显示其中存在的消息框。显示一次消息框后如何停止执行。给我一个解决方案。谢谢。

【问题讨论】:

  • 您要关闭应用程序吗?

标签: c# wpf function c#-4.0 c#-3.0


【解决方案1】:

你可以返回一个布尔值:

public bool TransactFileCreation()
{
    if (BatchFolderPath == null)
    {
         WPFMessageBox.Show("test", "Select a Batch folder");
         return false;
    }
    // code..
    return true;
}

然后你这样调用:

public void CreateFileOutput(object parameter)
{
    if (!TransactFileCreation())
        return;

    WPFMessageBox.Show("test", "Process completed successfully.");
}

【讨论】:

    【解决方案2】:

    通常,您会从 TransactFileCreation 返回一个 bool,以告知操作是否成功。

    或者在严重的情况下你会抛出一个异常,但这只是针对非常规错误流。

    【讨论】:

      【解决方案3】:

      您可以使用Application.Current.Shutdown(); 随时退出您的应用程序。

      public void TransactFileCreation()
      {
          if (BatchFolderPath == null)
          {
               WPFMessageBox.Show("test", "Select a Batch folder");
               Application.Current.Shutdown();
          }
          // code..
      }
      

      【讨论】:

        【解决方案4】:

        创建 TransactFileCreation() 以返回 bool。

         public void CreateFileOutput(object parameter) 
            { 
                TransactFileCreation()? WPFMessageBox.Show("test", "Process completed successfully."):WPFMessageBox.Show("test", "Select a Batch folder");
            } 
        
            public boolTransactFileCreation() 
            { 
                return BatchFolderPath == null 
        
            } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-13
          • 2021-11-15
          • 2018-05-31
          • 2020-12-01
          • 2017-04-25
          • 1970-01-01
          相关资源
          最近更新 更多