我的脚本正在尝试在 Blazorise Carousel 组件中显示图像。编译期间没有错误,图像路径上没有错误,但是加载页面时,出现以下错误。我在网上搜索,但找不到与此错误相关的任何内容。
错误消息:“InvalidOperationException:无法为“Blazorise.Carousel”类型的属性“LocalizerService”提供值。没有“Blazorise.Localization.ITextLocalizerService”类型的已注册服务。”
下面这部分发生错误。
<Carousel Interval="5000">
@foreach (var imagePath in imagePaths)
{
<CarouselSlide>
<img src="@($"images/{imagePath}")" class="d-block w-100" alt="Slide">
</CarouselSlide>
}
</Carousel>
完整代码如下。
<!-- Index.razor -->
@page "/"
@using System.IO
@using Blazorise
@using Blazorise.Localization
<h1>Image List</h1>
@if (imagePaths != null && imagePaths.Any())
{
<div class="image-list">
@* @foreach (var imagePath in imagePaths)
{
<img src="@GetRelativePath(imagePath)" alt="Image">
} *@
<Carousel Interval="5000">
@foreach (var imagePath in imagePaths)
{
<CarouselSlide>
<img src="@($"images/{imagePath}")" class="d-block w-100" alt="Slide">
</CarouselSlide>
}
</Carousel>
</div>
}
else
{
<p>No images found.</p>
}
@code {
private List<string> imagePaths;
protected override void OnInitialized()
{
var folderPath = "wwwroot/images"; // Path to your images folder
imagePaths = Directory.GetFiles(folderPath, "*.jpg")
.Concat(Directory.GetFiles(folderPath, "*.png"))
.ToList();
}
private string GetRelativePath(string absolutePath)
{
return absolutePath.Replace("wwwroot", string.Empty);
}
}
您可以尝试安装
Blazorise.Bootstrap
并在服务器项目和客户端项目中注册如下(如果有)。
builder.Services
.AddBlazorise()
.AddBootstrapProviders();