[英]ProgressDialogController mahapps, problems closing popup
I am creating a WPF project using Mahapps Metro .我正在使用 Mahapps Metro 创建一个 WPF 项目。
My problem is that I would launch from DetailPage.xaml frame , which is embedded in MainWindow.xaml , the wait modal mahapps provides.我的问题是我将从 DetailPage.xaml 框架启动,它嵌入在 MainWindow.xaml 中,等待模式 mahapps 提供。 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
我在框架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)我在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
).这里的问题是您正在执行“即发即弃”( async void
)。 In other words you are calling showMessaggeAsyncFromMainWindow
and rigth after you are calling closeMessaggeAsyncFromMainWindow
before dialog
is initiated.换句话说,您在启动dialog
之前调用closeMessaggeAsyncFromMainWindow
之后调用showMessaggeAsyncFromMainWindow
和 rigth。
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
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
.您正在等待对话框使用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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.