Nefarius.Blazor.EventAggregator
1.1.0
Prefix Reserved
dotnet add package Nefarius.Blazor.EventAggregator --version 1.1.0
NuGet\Install-Package Nefarius.Blazor.EventAggregator -Version 1.1.0
<PackageReference Include="Nefarius.Blazor.EventAggregator" Version="1.1.0" />
paket add Nefarius.Blazor.EventAggregator --version 1.1.0
#r "nuget: Nefarius.Blazor.EventAggregator, 1.1.0"
// Install Nefarius.Blazor.EventAggregator as a Cake Addin #addin nuget:?package=Nefarius.Blazor.EventAggregator&version=1.1.0 // Install Nefarius.Blazor.EventAggregator as a Cake Tool #tool nuget:?package=Nefarius.Blazor.EventAggregator&version=1.1.0
Nefarius.Blazor.EventAggregator
Lightweight Event Aggregator for Blazor.
Event aggregator is used for indirect component-to-component communication. In event aggregator pattern, you have message/event publishers and subscribers. In the case of Blazor, a component can publish its events and other component(
- can react to those events.
Note
Blazor.EventAggregator is completely based on the work done in Caliburn.Micro. The source code was copied from it and then altered to work with Blazor.
Also note that the library has only been tested with the server-side version of Blazor. There's no known issues with the WebAssembly-version of Blazor.
Getting Started
First register EventAggregator in app’s ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddEventAggregator();
}
The rest depends on if you’re using components with code-behinds (inheritance) or without inheritance. The following guidance is for code-behind scenarios.
Creating the publisher
To create an event publishing component, first inject IEventAggregator:
[Inject]
private IEventAggregator _eventAggregator { get; set; }
Then publish the message when something interesting happens:
await _eventAggregator.PublishAsync(new CounterIncreasedMessage());
Here’s a full example of a publisher:
public class CounterComponent : ComponentBase
{
[Inject]
private IEventAggregator _eventAggregator { get; set; }
public int currentCount = 0;
public async Task IncrementCountAsync()
{
currentCount++;
await _eventAggregator.PublishAsync(new CounterIncreasedMessage());
}
}
public class CounterIncreasedMessage
{
}
Creating the subscriber
To create an event subscriber, also start by injecting IEventAggregator:
[Inject]
private IEventAggregator _eventAggregator { get; set; }
Then make sure to add and implement the IHandle<TMessageType>
interface for all the event’s your component is
interested in:
public class CounterListenerComponent : ComponentBase, IHandle<CounterIncreasedMessage>
...
public Task HandleAsync(CounterIncreasedMessage message)
{
currentCount += 1;
return Task.CompletedTask;
}
Here’s full example of a subscriber:
public class CounterListenerComponent : ComponentBase, IHandle<CounterIncreasedMessage>
{
[Inject]
private IEventAggregator _eventAggregator { get; set; }
public int currentCount = 0;
protected override void OnInit()
{
_eventAggregator.Subscribe(this);
}
public Task HandleAsync(CounterIncreasedMessage message)
{
currentCount += 1;
return Task.CompletedTask;
}
}
Note about auto refresh
The library can try to automatically call subscriber component's StateHasChanged after it has handled the event. By default this functionality is disabled. You can enable it through options:
services.AddEventAggregator(options => options.AutoRefresh = true);
Auto refresh is based on reflection and it assumes that the subscriber inherits from ComponentBase.
Samples
The project site contains a full working sample of the code-behind model in the samples-folder.
Requirements
The library has been developed and tested using the following tools:
- .NET 7.0, 8.0 and 9.0
- Visual Studio 2022
Acknowledgements
Work is based on the code available in Caliburn.Micro.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.20)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.11)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.0 | 54 | 1/13/2025 |
1.1.0-pre002 | 46 | 1/12/2025 |
1.0.10 | 1,924 | 11/19/2022 |
1.0.8 | 207 | 11/19/2022 |