System.Windows.MessageBox自动关闭?

时间:2014-01-20 04:58:28

标签: c# wpf

我正在使用System.Windows.Forms.MessageBox作为下面的代码

System.Windows.Forms.MyMessageBox result = System.Windows.Forms.MyMessageBox.Show("You have to choose a folder!", "", MessageBoxButton.OK, MessageBoxImage.Information);

if (result == MessageBoxResult.OK)
{
    //Popup File Dialog
    System.Windows.Forms.DialogResult resultFd = System.Windows.Forms.DialogResult.Cancel;
    System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
    resultFd = folderDialog.ShowDialog();
    if (resultFd == System.Windows.Forms.DialogResult.OK)
    {
        //Do something
    }
}        

有时,Messagebox会在显示后自动关闭。

然后,条件(result == MessageBoxResult.OK)有时等于TRUE,有时等于FALSE

有人可以告诉我MessageBox自动关闭的原因吗?以及如何解决它?

非常感谢,

T& T公司

UPDATE:完整代码位于

之下
public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();      
        StartingWindow startingWindow = new StartingWindow();            
        startingWindow.RunAndUpdateProgressBar(InitializeAll);
        startingWindow.ShowDialog();
    }

    private void InitializeAll()
    {
        this.Dispatcher.Invoke(new Action(delegate()
        {
            //Do somethings
            startingWindow.UpdateProgressBar(35);
            //Do somethings
            startingWindow.UpdateProgressBar(65);

            System.Windows.Forms.MyMessageBox result = System.Windows.Forms.MyMessageBox.Show("You have to choose a folder!", "", MessageBoxButton.OK, MessageBoxImage.Information);
            if (result == MessageBoxResult.OK)
            {
                //Popup File Dialog
                System.Windows.Forms.DialogResult resultFd = System.Windows.Forms.DialogResult.Cancel;
                System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
                resultFd = folderDialog.ShowDialog();
                if (resultFd == System.Windows.Forms.DialogResult.OK)
                {
                    //Do something
                }
            }    
            startingWindow.UpdateProgressBar(100); //Set 100%, The startingWindow will be closed by  (***)
        }
    }
}

public partial class StartingWindow : Window
{
    private BackgroundWorker worker;
    public delegate void UpdateProgressBarDelegate();

    public StartingWindow()
    {
        InitializeComponent();    
        worker = new BackgroundWorker();
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
        worker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
        worker.WorkerReportsProgress = true;
    }

    public override void OnApplyTemplate()
    {
        base.ApplyTemplate();
    }

    public void RunAndUpdateProgressBar(UpdateProgressBarDelegate runThis)
    {
        worker.RunWorkerAsync(runThis);
    }

    public void UpdateProgressBar(int value)
    {
        worker.ReportProgress(value);
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            UpdateProgressBarDelegate updateProgressBarDelegate = e.Argument as UpdateProgressBarDelegate;
            updateProgressBarDelegate();

            double curProgressValue = 0;

            while (curProgressValue < 100)
            {
                this.Dispatcher.Invoke(new Action(delegate()
                {
                    curProgressValue = progressBar.Value;
                }));
                Thread.Sleep(200);
            }
        }
        catch (System.Exception ex)
        {
            Global.wrapper.WriteRuntimeLogs(ex.Message + "/n Stack Trace = " + ex.StackTrace);
            MessageBox.Show("An unexpected exception occurs when starting App!");
        }
    }

    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        TimeSpan duration = TimeSpan.FromSeconds(2);
        DoubleAnimation animation = new DoubleAnimation(e.ProgressPercentage, duration);
        progressBar.BeginAnimation(ProgressBar.ValueProperty, animation);    
        progressBar.Value = e.ProgressPercentage;
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar.Value = 0;
        Close(); //(***)
    }
}

UPDATE

namespace System.Windows.Forms
{
    public class MyMessageBox
    {
       public static MessageBoxResult Show
           (
                String messageBoxText,
                String caption,
                MessageBoxButton button,
                MessageBoxImage icon                
            )
        {
            MessageBoxManager.OK = Global.resourceManager.GetString("OK");
            MessageBoxManager.Cancel = Global.resourceManager.GetString("Cancel");
            MessageBoxManager.Abort = Global.resourceManager.GetString("Abort");
            MessageBoxManager.Retry = Global.resourceManager.GetString("Retry");
            MessageBoxManager.Ignore = Global.resourceManager.GetString("Ignore");
            MessageBoxManager.Yes = Global.resourceManager.GetString("Yes");
            MessageBoxManager.No = Global.resourceManager.GetString("No");

            MessageBoxManager.Register();
            MessageBoxResult result = System.Windows.MessageBox.Show(messageBoxText, caption, button, icon);          
            MessageBoxManager.Unregister();
            return result;
        }
    }
}

NOTE:当MessageBox自动关闭时,startingWindow仍在显示。

0 个答案:

没有答案