Using JSRuntime.InvokeAsync causes "Index was out of range" error on BlazorStrap Carousel

416 Views Asked by At

In my Blazor Server Application I have a page with a BlazorStrap Carousel and a button to download a zip file. The Carousel works fine and can loop through all of the images without issue until I click the button to download the zip file. The zip file downloads correctly but after that I get this error in the console:

Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List`1.get_Item(Int32 index) at BlazorStrap.BSCarouselBase.AnimationEnd(BSCarouselItemBase sender)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Html for my carousel and download files button:

<BlazorStrap.BSCarousel NumberOfItems="@slideshowImages.Count" style="width: 23vw;">
    <div class="carousel-inner">
        @foreach (var item in slideshowImages)
        {
            <BlazorStrap.BSCarouselItem Src="@item.Source" Alt="@item.Alt" style="width: 100%;">
                @if (bShowImageTitle == true)
                {
                    <BlazorStrap.BSCarouselCaption CaptionText="@item.Caption.Split('.')[0]" />
                }
            </BlazorStrap.BSCarouselItem>
        }
    </div>
    <BlazorStrap.BSCarouselControl CarouselDirection="BlazorStrap.CarouselDirection.Previous" NumberOfItems="@slideshowImages.Count" />
    <BlazorStrap.BSCarouselControl CarouselDirection="BlazorStrap.CarouselDirection.Next" NumberOfItems="@slideshowImages.Count" />
</BlazorStrap.BSCarousel>

<button class="btn btn-primary bold" type="button" @onclick="(e => btnDownloadAllClick())">DOWNLOAD FILES</button>

Code for getting carousel items:

List<Slideshow> slideshowImages = new List<Slideshow>();

public class Slideshow
{
    public string Source { get; set; }
    public string Alt { get; set; }
    public string Caption { get; set; }
    public string Header { get; set; }
}

public int iImageCount;

protected override void OnInitialized()
{
    //here i'm getting the images from a folder in azure storage, this works fine
    List<string> tempList = clsBlobService.ListBlobs(folderPath);

    foreach (var img in tempList)
        {
            Slideshow tempItem = new Slideshow
            {
                Source = rootPath + img,
                Alt = img.Split('/').Last(),
                Caption = img.Split('/').Last()
            };

            slideshowImages.Add(tempItem);
        }

        iImageCount = slideshowImages.Count;
}

Code for when the download files button is clicked:

public async void btnDownloadAllClick()
{   
    await JSRuntime.InvokeVoidAsync("open", "/Temp/MyZipFile.zip");
}

Edit: I forgot to add that my render-mode is Server and not ServerPrerendered.

2

There are 2 best solutions below

2
On BEST ANSWER

Your error happens at BlazorStrap.BSCarouselBase.AnimationEnd().

But on GitHub I can't find BSCarouselBase and the record suggests your bug was fixed.

So, update your package(s).

0
On

try this

 
    @if (bShowImageTitle == true 
                && item.Caption.Split('.')!=null  
                   && item.Caption.Split('.').Length > 0  
        )
     {
      <BlazorStrap.BSCarouselCaption CaptionText="@item.Caption.Split('.')[0]" />
      }