问题描述
我在Blazor
应用中关注this guide,以显示模式对话框。我知道Blazored.Modal,此处并未将其用于学习目的。
这里的要点是,我想将其用于某种形式的用户验证,并且仅在用户同意时才执行代码。我使用ModalService
在另一个MyBackgroundService
中显示提示,做了一些需要用户选择的事情。
这是主页代码:
@page "/"
@inject ModalService _modalService
@inject MyBackgroundService _myService
<div>
<button @onclick="onShowClick" class="btn btn-primary">Show</button>
<button @onclick="onRunClick" class="btn btn-primary">Run</button>
</div>
@code {
protected async Task onShowClick() {
// shows MyControl in a modal form => working just great!
_modalService.Show(typeof(MyControl));
}
protected async Task onRunClick() {
await _myService.Run();
}
}
这里是ModalService
类的代码:
public class ModalService {
public event Action<Type> OnShow;
public event Action OnClose;
public void Show(Type contentType) {
if (contentType.BaseType != typeof(ComponentBase)) {
throw new ArgumentException($"{contentType.FullName} must be a Blazor Component");
}
OnShow?.Invoke(contentType);
}
public void Close() {
OnClose?.Invoke();
}
}
这是MyBackgroundService
类的示例代码:
public class MyBackgroundService {
private readonly ModalService _modalService;
public CalSyncerService(ModalService modalService) {
_modalService = modalService;
}
public async Task Run() {
var processResult1 = await firstLongProcess();
string userAnswer = "Ok";
if (processResult1 != true) {
// something went wrong so far => it might be risky to continue => ask user if Ok...
// of course,we're not awaiting the user answer here,this is what I need to correct!!
_modalService.Show(typeof(MyControl));
}
if (userAnswer == "Ok") {
await secondProcess();
}
}
}
等待用户回答的一种干净方法是什么?使用一种动作,或者甚至更好的一种async
方法,显示模式对话框并等待对话框关闭以返回答案?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)