ProgressDialogController mahapps,关闭弹出窗口的问题

时间:2016-02-03 08:47:20

标签: c# wpf xaml modal-dialog mahapps.metro

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

我的问题是我将从DetailPage.xaml框架启动,该框架嵌入在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();
}

我的错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

这里的问题是你正在做一个" Fire and Forget" (async void)。换句话说,在showMessaggeAsyncFromMainWindow启动之前致电closeMessaggeAsyncFromMainWindow之后,您正在致电dialog并进行操作。

解决方案:

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打开和关闭对话框。

快乐编码:)

答案 1 :(得分:0)

如果有人想使用对话框的“取消”按钮将其关闭

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