• Stars
    star
    117
  • Rank 293,258 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight Event Aggregator for Blazor (Razor Components).

Blazor.EventAggregator

Blazor.EventAggregator is a lightweight Event Aggregator for Blazor.

  • 3/2020: Updated to work with .NET Core v3.1.0

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, component can publish its events and other component(s) can react to those events.

NuGet

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 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 Core 3.1
  • Visual Studio 2019

Acknowledgements

Work is based on the code available in Caliburn.Micro.

More Repositories

1

Blazor.Animate

Easily add fade, slide and zoom-effects into your Blazor applications.
C#
270
star
2

graze

Static site generator using Razor.
C#
151
star
3

blog

Source codes from my blog posts
C#
53
star
4

Blazor.AdaptiveCards

Adaptive Cards for Blazor
C#
49
star
5

SQLLint

SQLLint is a web based reformatter and beautifier for SQL.
CSS
40
star
6

socketex

TcpClient for Windows Phone
C#
16
star
7

GrazeDocs

Static site generator for creating documentation for your projects
C#
15
star
8

Blazor.Page

Page base component for Blazor pages. Easily update page title and optionally inject datacontext into your page.
C#
9
star
9

vssbindingremover

VSS Binding Remover strips out the Visual Source Safe related sections from the Visual Studio solution and project files
C#
9
star
10

NSQLFormatter-dotnet

Beautify your SQL easily. NSQLFormatter is an open-source SQL Beautifier written with C# and .NET Framework 4.0. Project is based 100% on the work done in the NHibernate-project.
C#
9
star
11

grazewp7

Windows Phone 7 app marketing web site Generator using Graze and Twitter Bootstrapper.
JavaScript
8
star
12

UWPResponsiveXAMLLayoutGridAdaptiveTrigger

Example of how to use repositioning to create a responsive layout for your XAML Universal Windows app (UWP) using Grid and AdaptiveTrigger.
C#
7
star
13

UWP.MDI

Multiple document interface (MDI) support for UWP applications
C#
7
star
14

ircbot-dotnet

Irc bot built with easy extensibility in mind. Written with C#, the bot provides a platform on which to add new features and functionality easily.
C#
7
star
15

Caliburn-Micro-WinRT-Callisto-Helpers

Helper classes for showing Callisto dialogs with Caliburn.Micro
C#
4
star
16

AttachToDotnet

Extension for Visual Studio 2017/2019 which makes it easier to attach to the correct dotnet.exe process
C#
4
star
17

SubtitleProvider

Subtitle plugin for MediaBrowser. The plugin adds an automatic subtitle downloading to MediaBrowser.
C#
4
star
18

node-web-api-benchmark

Codes for the Node.js vs ASP.NET Web Api benchmarks
C#
4
star
19

Blazor.CommandButton

Blazor.CommandButton is a lightweight button component for Blazor. CommandButton provides support for setting a guard method or property to the button.
HTML
3
star
20

irc7

The Windows Phone IRC chat app
3
star
21

AzureBlobBridgeIFTTT

Azure Blob Bridge for IFTTT provides an easy way to use Azure Blob storage as a IFTTT Action.
C#
3
star
22

OcelotIdentityServerAzureAdWithApi

Demo for a scenario where API is accessed through Ocelot gateway and Ocelot uses IdentityServer and Azure Ad for authentication
C#
3
star
23

wordpress-sql-azure

Wordpress with SQL Azure
PHP
2
star
24

NXMLFormatter

NXMLFormatter is an open-source XML Beautifier written with C#.
C#
2
star
25

NCompileBench

Compilation benchmark for Windows. How fast can your computer compile .NET code?
C#
2
star
26

softwaremkwebsource

www.softwaremk.org
CSS
1
star
27

grazeblog

Blog platform based on Graze templating engine.
1
star
28

websitetest

1
star
29

UWPEventAggregator

Simple event aggregator for Multi Window UWP apps
C#
1
star
30

ipsnoop

The website for IP Snoop app for Windows Phone 7
1
star
31

uwpusercontrollistdp

C#
1
star
32

durandal-phonegap-windows-phone-sample

A sample which shows how to use Durandal in a PhoneGap based Windows Phone app.
JavaScript
1
star
33

softwaremkassets

JavaScript
1
star
34

UWP-SemanticZoom-GridView

Sample UWP app which shows how to use SemanticZoom with GridView
C#
1
star
35

link

Standalone host for the ASP.NET Web Api
1
star
36

ampparit

Ampparit.com Reader website
1
star
37

uwpusercontrolbinding

C#
1
star
38

marketwatcher

Windows Phone class library which fetches reviews for a desired app from the Windows Marketplace.
C#
1
star
39

softwaremkwebsite

www.softwaremk.org
JavaScript
1
star
40

aurelia-uwp

Sample of creating an Aurelia-based UWP app
JavaScript
1
star
41

movieposters

Website for Movie Poster for Windows Phone 7
1
star