-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async Task and async method not working #7
Comments
@peterhym21 async ValueTask Cancel() => await BlazoredModal.CancelAsync();
// 👆 Use `ValueTask` instead of `Task`. |
Hi @jsakamoto Thanks for the help, but I don't understand why this is by design can you maybe clarify and help me to fined a better solution for my problem? Because now you cant use it in with a Task Like see below for old and new HotKeys versions code that I am talking about Old Hotkeys @inject HotKeys HotKeys
@implements IDisposable
<div class="modal ">
<div class="modal-content bg-danger">
<div class="modal-header">
<h3 class="modal-title">Error</h3>
</div>
<div class="modal-body">
<p class="message">@Message</p>
</div>
<div class="modal-footer">
<RadzenButton Click="OK" ButtonStyle="ButtonStyle.Primary" class="modal-button">OK</RadzenButton>
<RadzenButton Click="Cancel" ButtonStyle="ButtonStyle.Light" class="modal-button">Cancel</RadzenButton>
</div>
</div>
</div>
@code {
[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; } = default!;
[Parameter] public string? Message { get; set; }
HotKeysContext? HotKeysContext;
protected override async Task OnInitializedAsync()
{
this.HotKeysContext = this.HotKeys.CreateContext()
.Add(ModKeys.None, Keys.ESC, Cancel);
}
async Task OK() => await BlazoredModal.CloseAsync();
async Task Cancel() => await BlazoredModal.CancelAsync();
public void Dispose() => this.HotKeysContext.Dispose();
} New HotKeys2 @inject HotKeys HotKeys
@implements IDisposable
<div class="modal ">
<div class="modal-content bg-danger">
<div class="modal-header">
<h3 class="modal-title">Error</h3>
</div>
<div class="modal-body">
<p class="message">@Message</p>
</div>
<div class="modal-footer">
<RadzenButton Click="OK" ButtonStyle="ButtonStyle.Primary" class="modal-button">OK</RadzenButton>
<RadzenButton Click="Cancel" ButtonStyle="ButtonStyle.Light" class="modal-button">Cancel</RadzenButton>
</div>
</div>
</div>
@code {
[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; } = default!;
[Parameter] public string? Message { get; set; }
HotKeysContext? HotKeysContext;
protected override async Task OnInitializedAsync()
{
this.HotKeysContext = this.HotKeys.CreateContext()
.Add(ModCode.None, Code.Escape, CancelHotkey);
}
async Task OK() => await BlazoredModal.CloseAsync();
async Task Cancel() => await BlazoredModal.CancelAsync();
async ValueTask CancelHotkey() => await BlazoredModal.CancelAsync();
public void Dispose() => this.HotKeysContext.Dispose();
} |
Hi @peterhym21,
The first reason is "performance". See also: "ValueTask vs. Task" https://medium.com/@karol.rossa/valuetask-vs-task-5fb4f9c6517 The second reason is that I don't want to spend much time implementing the Those are reasons "why this is by design". By the way, if you must the callback method be a ...
@code {
...
protected override async Task OnInitializedAsync()
{
this.HotKeysContext = this.HotKeys.CreateContext()
// Wrap your `Task` async method with 👇 async/await lambda like below.
.Add(ModCode.None, Code.Escape, async () => await Cancel());
}
...
async Task Cancel() => await BlazoredModal.CancelAsync();
... Moreover, you can also implement your custom extension P.S. .Add(ModCode.None, Code.Escape, async () => await Cancel()); You can rewrite it shorter without the .Add(Code.Escape, async () => await Cancel()); Happy Coding! |
When trying to use is with a async task it does not work:
gives these errors
works fine if I use a void method
@jsakamoto any help?
The text was updated successfully, but these errors were encountered: