问题描述
我为Winforms应用程序创建了一个自定义的Toast通知,每当返回自定义结果时,我都会使用ShowMessage扩展来显示该通知。
在登录屏幕上,我有一个解锁应用程序按钮,只要密码不正确,该按钮就会显示烤面包机消息。
当我在result.ShowMessage()之前放置一个断点时;通知出现。但是当我删除断点时,它不再出现。我不更改任何调试值。
我该如何解决?我确实尝试实现thread.sleep,但是它没有任何改变,仅当我插入一个断点并继续处理该断点时,该通知才会出现。
值得注意的是,该通知在应用程序中的其他任何地方都可以正常工作。
解锁应用程序的代码:
private void UnlockApplication()
{
var result = new Business.Server.User().Get(_loginModel.UserName,_loginModel.Password.Encrypt());
if (result.IsSuccessful)
{
// Perform log in process
}
else
{
result.ShowMessage(); //Only works when I put breakpoint here
}
}
ShowMessage扩展代码
public static void ShowMessage<T>(this Models.Result<T> result) where T : class
{
Helpers.ToasterNotificationHelper.ShowNotification(result.ResultTypeKey.ToDescription(),result.Message,result.ResultImage(),result.ResultColor(),result.ResultTypeKey == Enums.ResultTypeEnum.Warning ? 5000 : 2500);
}
public static void ShowNotification(string header,string message,Image icon,Color backgroundColor,int durationInMilliseconds = 1000)
{
if (Application.OpenForms[0] is MyApplication)
{
var toasterNotification = new ToasterNotificationControl(header,message,icon,backgroundColor);
(Application.OpenForms[0] as MyApplication).toastNotificationCollectionControl1.AddNotification(toasterNotification,durationInMilliseconds);
}
}
我的Toast Notification控件是一个DevExpress弹出面板控件,已添加到Main应用程序表单中
解决方法
您的“ Get(_loginModel.UserName ...”似乎是异步的。请尝试执行下一个操作以使呼叫等待:
private async System.Threading.Tasks.Task UnlockApplicationAsync()
{
var result = await new Business.Server.User().Get(_loginModel.UserName,_loginModel.Password.Encrypt());
...