Injecting dependencies into Blazor components

Defining our dependency Before we can inject a dependency, we’ll need to create one. We’ll use the age old ToDo example but, don’t worry, we won’t be creating a To Do application. First create a basic ToDo class. Next, we’ll create a class that a Blazor Page or Component might need. In this case it […]

Binding directives

We covered Directives earler – if you are unfamiliar with directives, please read the section about them before you continue. We previously covered Directives, and Directive Attributes. In this section we will cover assigning values to Directive Attributes by demonstrating how they are utilised when using two-way binding. As a quick recap, a Directive is […]

Literals, expressions, and directives

Note that this section does not cover Razor mark-up in general. It will not cover things such as conditional output, loops, and so on. That subject and is covered quite extensively in other places, both on the web and in books. When consuming a component we can pass it information as parameters. These parameters can […]

Calling static .NET methods

As well as invoking methods on .NET object instances, Blazor also enables us to invoke static methods. The next example will show how to call into .NET from JavaScript and retrieve a specific setting it might need for an API call – for example, for Google Analytics. The benefit of reading JavaScript settings from the […]

Lifetimes and memory leaks

If we run the app we created in Calling .NET from Javascript and inspect browser console window, we’ll see that when we navigate to another page JavaScript is still calling back our component. What’s worse, if we look in our Visual Studio output window we’ll see that our component is still being invoked and outputting […]

Type safety

In the section Calling .NET from JavaScript, you may have noticed that line 6 of our JavaScript calls toString() on our randomly generated number before passing it to .NET Despite object types being quite interchangeable in JavaScript, they are not so interchangeable when they are passed to our .NET Invokable method. When invoking .NET, make […]

Updating the document title

In the section Creating a Blazor layout it we saw how a Blazor app lives within a HTML (or cshtml) document, and only has control over the content within the main application element. Websites that are not single-page applications can specify text to appear in the browser’s tab by adding a <title> element within the […]

Descending from InputBase<T>

The InputBase<T> component is the abstract class the various Blazor input controls descend from. This class adds additional functionality above that of standard HTML <input> elements, such as validation – which we’ll cover later. Because of this, it is recommended we descend components from this base class if we intend them to act as user […]

Constraining route parameters

In addition to being able to specify URL templates that include parameters, it is also possible to ensure Blazor will only match a URL to a component if the value of the parameter meets certain criteria. For example, in an application where purchase order numbers are always integers we would want the parameter in our […]

Calling .NET From JavaScript

Sometimes our .NET application code needs to be executed from JavaScript. Blazor enables us to asynchronously call methods on instances of objects, or static methods on classes. Identifying invokable .NET code Blazor does not allow JavaScript to call just any static or instance method in our .NET code. There are conditions The method must be […]