C#异步方法不是';t运行模式
本文关键字:运行 模式 异步方法 | 更新日期: 2023-09-27 18:26:03
我的源代码中存在一个问题,导致异步方法不模态。我使用的是Mahapps Metro Framework,我有一个Logger类,里面有两个异步方法:
public class Logger : ILogger {
public void outputMessage(string message) {
Console.WriteLine(message);
}
public void outputUserMessage(string message) {
MessageBox.Show(message);
}
public async void outputMetroUserMessage(object window, String title, String message) {
MetroWindow mWindow = (MetroWindow)window;
await mWindow.ShowMessageAsync(title, message);
}
public async void outputMetroUserMessageWithHidingMDI(object window, string title, string message) {
UIGlobals.MainPageMdiChild.Visibility = Visibility.Hidden;
MetroWindow mWindow = (MetroWindow)window;
await mWindow.ShowMessageAsync(title, message);
UIGlobals.MainPageMdiChild.Visibility = Visibility.Visible;
}
}
还有一些其他类的方法调用Logger方法。示例:
public partial class Login : MetroWindow {
public Login() {
InitializeComponent();
}
private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
DoLogin();
}
private void DoLogin() {
String email = txtEMail.Text;
String password = txtPassword.Password;
if (String.IsNullOrWhiteSpace(email)) {
Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(103), UserErrorMessageController.GetMessageByID(103));
} else if (String.IsNullOrWhiteSpace(password)) {
Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(104), UserErrorMessageController.GetMessageByID(104));
} else {
.
.
.
}
}
}
简介创建者:
public partial class ProfileCreator : MetroWindow {
public ProfileCreator(Network tempNetwork, UserProfile tempProfile) {
InitializeComponent();
.
.
.
}
private void btnSave_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
// TODO: Set cancelling when someting is missing
Save();
}
private void Save() {
getUserProfileValuesFromWindow();
Globals.TheSerializer.Serialize(tempProfile, Globals.PathToTemporaryFiles + "MyProfile.xml");
tempNetwork.NetworkParticipants.Add(tempProfile.ParticipantID);
Globals.TheSerializer.Serialize(tempNetwork, Globals.PathToTemporaryFiles + "MyNetwork.xml");
Globals.Logger.outputMetroUserMessage(this, "Erfolg", "Ihr Testsystem wurde erfolgreich angelegt.'nDrücken Sie erneut auf '"Testen'" und loggen Sie sich ein.");
Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(104), UserErrorMessageController.GetMessageByID(104));
}
当我在Login类中调用Loggers方法时,这些方法按预期运行模态,但如果我从ProfileCreator调用它们,它们似乎不是模态的。我试图弄清楚,但我看不出类和参数有什么不同。也许你会看到一些我没有看到的东西。
谢谢你的帮助!
问题是您没有使用Task
。例如,最佳实践是从所有未从WPF中的用户单击事件调用的async
方法返回Task
。你的代码应该是这样的:
public class Logger : ILogger
{
public void outputMessage(string message)
{
Console.WriteLine(message);
}
public void outputUserMessage(string message)
{
MessageBox.Show(message);
}
public Task<MessageDialogResult> outputMetroUserMessage(object window, String title, String message)
{
MetroWindow mWindow = (MetroWindow)window;
return mWindow.ShowMessageAsync(title, message);
}
public async Task outputMetroUserMessageWithHidingMDI(object window, string title, string message)
{
UIGlobals.MainPageMdiChild.Visibility = Visibility.Hidden;
MetroWindow mWindow = (MetroWindow)window;
await mWindow.ShowMessageAsync(title, message);
UIGlobals.MainPageMdiChild.Visibility = Visibility.Visible;
}
}
并按如下方式消费:
public partial class Login : MetroWindow
{
public Login()
{
InitializeComponent();
}
private async void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
await DoLogin();
}
private async Task DoLogin()
{
String email = txtEMail.Text;
String password = txtPassword.Password;
if (String.IsNullOrWhiteSpace(email))
{
await Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(103), UserErrorMessageController.GetMessageByID(103));
}
else if (String.IsNullOrWhiteSpace(password))
{
await Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(104), UserErrorMessageController.GetMessageByID(104));
}
else
{
// ...
}
}
}
请注意,我只是在UI的事件处理程序上使用async void
,这应该是您使用该模式的唯一地方。请通读一遍以获得解释。
这里是最后一部分,再次不要使用async void,除非您是UI交互的事件处理程序的方法。
public partial class ProfileCreator : MetroWindow
{
public ProfileCreator(Network tempNetwork, UserProfile tempProfile)
{
InitializeComponent();
// ...
}
async void btnSave_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// TODO: Set cancelling when someting is missing
await Save();
}
async Task Save()
{
getUserProfileValuesFromWindow();
Globals.TheSerializer.Serialize(tempProfile, Globals.PathToTemporaryFiles + "MyProfile.xml");
tempNetwork.NetworkParticipants.Add(tempProfile.ParticipantID);
Globals.TheSerializer.Serialize(tempNetwork, Globals.PathToTemporaryFiles + "MyNetwork.xml");
await Globals.Logger.outputMetroUserMessage(this, "Erfolg", "Ihr Testsystem wurde erfolgreich angelegt.'nDrücken Sie erneut auf '"Testen'" und loggen Sie sich ein.");
await Globals.Logger.outputMetroUserMessage(this, UserErrorMessageController.GetTitleByID(104), UserErrorMessageController.GetMessageByID(104));
}
}
这是因为您两次调用异步方法"outputMetroUserMessage",而不等待第一次的结果。你可以这样重新定义你的方法:
public Task<MessageDialogResult> OutputMetroUserMessage(object window, string title, string message)
{
MetroWindow mWindow = (MetroWindow)window;
return mWindow.ShowMessageAsync(title, message);
}
然后等待消息输出:
private async void button1_Click(object sender, RoutedEventArgs e)
{
await this.OutputMetroUserMessage(this, "Title", "Message1");
await this.OutputMetroUserMessage(this, "Title", "Message2");
}
第二个调用将在用户确认消息之后执行。
希望能有所帮助。