visit
I was heads-down working on some Blazor UI for an application I’m building, and it’s the motivation behind whipping up another quick article on MudBlazor list items. The problem was pretty simple: I just wanted to wire up some event handlers to some buttons that I had on my list items.
Here’s what the code looked like:
@page "/items"
@using DevLeader.Services
@using DevLeader.Services.Items
@inject IItemsService ItemsService
<PageTitle>Dev Leader Is Awesome!</PageTitle>
<h1>Dev Leader Is Awesome!</h1>
@if (_itemsCollection == null)
{
<p><em>Loading...</em></p>
}
else if (_itemsCollection.Count == 0)
{
<p><em>No items found.</em></p>
}
else
{
<MudList>
@foreach (var item in _itemsCollection)
{
<MudListItem>
<MudCard>
<MudCardHeader>
@item.Title
</MudCardHeader>
<MudCardContent>
<MudButtonGroup xs="12" sm="6">
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="e => ApproveButton_OnClickAsync(item)">
Approve
</MudButton>
<MudButton Variant="Variant.Filled"
Color="Color.Secondary"
OnClick="async e => await RejectButton_OnClickAsync(item)">
Reject
</MudButton>
</MudButtonGroup>
</MudCardContent>
</MudCard>
</MudListItem>
}
</MudList>
}
@code {
private IReadOnlyList<Item>? _itemsCollection;
protected override async Task OnInitializedAsync()
{
await Task.Yield();
var endDateTimeUtc = DateTime.UtcNow.AddDays(1);
var startDateTimeUtc = DateTime.UtcNow.AddDays(-7);
_itemsCollection = await ItemsService
.GetItemsAsync(
startDateTimeUtc: startDateTimeUtc,
endDateTimeUtc: endDateTimeUtc,
cancellationToken: CancellationToken.None)
.ConfigureAwait(false);
}
private async Task ApproveButton_OnClickAsync(Item feedItem)
{
await Task.Yield();
}
private async Task RejectButton_OnClickAsync(Item feedItem)
{
await Task.Yield();
}
}
You can see that I was even fooling around with trying to see different implementations of the — because they just were NOT triggering no matter what I did. But that turned out to be a red herring because the event handlers not triggering were a symptom of the problem.
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]Unhandled exception rendering component: Cannot provide a value for property 'ServiceImplementation' on type 'MyProject.Pages.MyPage'. There is no registered service of type 'MyProject.IService'.System.InvalidOperationException: Cannot provide a value for property 'MyService' on type 'MyProject.Pages.MyPage'. There is no registered service of type 'MyProject.IService'.
I figured if I have an exception being thrown on the page, perhaps I shouldn’t put my energy into the async event handlers anymore on the MudBlazor list items… Odds are, I had a bigger problem to sort out, and that bigger problem was just causing the rest of the page to not load correctly. Spoiler alert: the odds were correct.
@code {
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: InteractiveAuto;
}
@code {
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
// private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
// ? null
// : InteractiveAuto;
private IComponentRenderMode? RenderModeForPage => InteractiveServer;
}