ProgressDialogController mahapps,关闭弹出窗口时出现问题

ProgressDialogController mahapps, problems closing popup

我正在使用 Mahapps Metro 创建一个 WPF 项目。

我的问题是我会从 DetailPage.xaml frame 启动,它嵌入在 MainWindow.xaml 中,等待模式 mahapps 提供。 所以我做了打开和关闭两种方法,但最后他说那不再是对话框。 有人有解决办法吗?

框架中我的代码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();
}

我的代码在 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();
}

我的错误:

这里的问题是你在做 "Fire and Forget" (async void)。换句话说,您正在调用 showMessaggeAsyncFromMainWindow 并且在 dialog 启动之前调用 closeMessaggeAsyncFromMainWindow 之后。

解决方案:

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();
}

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();
}

使用此解决方案,您无需执行 "Fire and Forget"。您正在等待使用 await.

打开和关闭对话框

编码愉快:)

如果有人想使用对话框取消按钮关闭它

        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();

这是取消事件

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