Upload files with drag & drop or paste from clipboard in Blazor

  • Gérald Barré
To upload a file you can use the FileUpload control. This control generates a <input type="file"> element and allows you to upload a file. To get a good user experience, you also need to support dropping files from the explorer or pasting images from the clipboard. The final result should look like this: First, you need to create a JavaScript file to register some events to handle drag and drop events and… [read more]

Removing elements after an animation in Blazor

  • Gérald Barré
There are many cases where you want to smoothly remove an element from the UI. A solution is to use an animation to fade out the element before removing it. You can start the animation by adding a class to the element you want to remove. Then, you can use the transitionend or animationend event to actually remove the element. First, you need to register the transitionend event handler, so you can use it… [read more]

How to measure elapsed time without allocating a Stopwatch

  • Gérald Barré
There are many cases where you need to compute the execution time of a method. The classic way is to use the Stopwatch class. Stopwatch is a class, so instantiating it may increase the pressure on the Garbage Collector. In the case of a single-threaded application, you can reuse the instance to avoid creating lots of instances. However, in a multi-threaded application, it may be more complicated to reuse… [read more]

Infinite scrolling in Blazor

  • Gérald Barré
Infinite scrolling is a way to automatically loads data when you reach the end of the page. It allows you to continue scrolling indefinitely. The method is often used in social media feeds or blogs. In this post, we'll create a Blazor component that you can use like the following: <InfiniteScrolling ItemsProvider="GetItems"> <ItemTemplate Context="item"> <p>Item @item</p> </ItemTemplate> <LoadingTemplate>… [read more]

Different ways to check if a value is null in C#

  • Gérald Barré
In .NET there are multiple ways to check if a value is null. But all of them are not equivalent. Let's look at the different ways to check if an object is null: object obj = ...; obj == null; object.Equals(obj, null); object.ReferenceEquals(obj, null); obj is null; // Equivalent to ReferenceEquals(obj, null) (object)obj == null; // Equivalent to ReferenceEquals(obj, null) obj is not object; // Equivalent… [read more]