简体   繁体   中英

ProgressDialogController mahapps, problems closing popup

I am creating a WPF project using Mahapps Metro .

My problem is that I would launch from DetailPage.xaml frame , which is embedded in MainWindow.xaml , the wait modal mahapps provides. So I did two methods that open and close , but at the end he says that is no longer the dialog . Does anyone have solutions ?

my code in frame DetailPage.xaml

private void Meeting_Click(object sender, RoutedEventArgs e)
{
    MainWindow w = (MainWindow)App.Current.MainWindow;
    w.showMessaggeAsyncFromMainWindow();


    var MIDClick = sender as Button;
    String MID = MIDClick.Tag as String;

    ...mycode...                

    w.closeMessaggeAsyncFromMainWindow();
}

my code in MainWindow.xaml (MetroWindow)

public ProgressDialogController dialog;

public async void showMessaggeAsyncFromMainWindow()
{
    dialog = await this.ShowProgressAsync(Properties.strings.attendi, Properties.strings.aggiornamentoMeetingsInCorso, false) as ProgressDialogController;
}

public async void closeMessaggeAsyncFromMainWindow()
{
    await dialog.CloseAsync();
}

my error:

在此处输入图片说明

The problem here is that you are doing a "Fire and Forget" ( async void ). In other words you are calling showMessaggeAsyncFromMainWindow and rigth after you are calling closeMessaggeAsyncFromMainWindow before dialog is initiated.

Solution :

private async void Meeting_Click(object sender, RoutedEventArgs e)
{
    MainWindow w = (MainWindow)App.Current.MainWindow;
    await w.showMessaggeAsyncFromMainWindow();


    var MIDClick = sender as Button;
    String MID = MIDClick.Tag as String;

    ...mycode...                

    await w.closeMessaggeAsyncFromMainWindow();
}

the code in your MainWindow.xaml

public ProgressDialogController dialog;

public async Task showMessaggeAsyncFromMainWindow()
{
    dialog = await this.ShowProgressAsync(Properties.strings.attendi, Properties.strings.aggiornamentoMeetingsInCorso, false) as ProgressDialogController;
}

public async Task closeMessaggeAsyncFromMainWindow()
{
    await dialog.CloseAsync();
}

With this solution you are not doing a "Fire and Forget". You are waiting for the dialog to open and close with the await .

Happy coding :)

If someone wants to close it using the dialog cancel Button

        var settings = new MetroDialogSettings()
        {

            NegativeButtonText = "cancel",
            DialogMessageFontSize = 25,
            DialogTitleFontSize = 25,
            DialogResultOnCancel = MessageDialogResult.Canceled,

        };
        ProgressDialogController controller =
                await this.ShowProgressAsync("Title", "Message", true, settings);
        controller.SetCancelable(true);

        controller.Canceled += controller_Canceled;

        //after your job

        if(controller.IsOpen)
             await controller.CloseAsync();

and this is the cancel event

    private async void controller_Canceled(object sender, EventArgs e)
    {
        ProgressDialogController controller_ = (ProgressDialogController)sender;
        await controller_.CloseAsync();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM