Yomodo / blazor-utilities

Messaging is a repository that represents the MessagingCenter service available in Xamarin as a package available for all Blazor developers that allows them to send data between components efficiently

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to Messaging Center Blazor Utitlity!

drawing

AKSoftware.Blazor.Utilities

⚑ ⚑ πŸš€ Send data across your components fluently with the Messaging Center ⚑ ⚑ πŸš€

Paramters, Cascading Parameters and Event Callbacks are ways to send data across components but MessagingCenter is much easier and straightforward MessagingCenter

If you are a Xamarin.Forms developer, you definitely passed across the MessagingCenter class that was being used to send messages across objects, this class helps a lot by pushing data across components in the system effiecently. And now for Blazor Messaging Center is a great tool to use to solve the issue of sending the data across components and updating them. Using MessagingCenter you will be able to send data from one component to another or to a set of other components just with one line of code despite the relationship between those components (Parent - Child .etc...) The following illustration shows the benefits of using MessagingCenter to send data between the components.

Messaging Center Quick Explanation:

enter image description here

Package Content:

For now package the AKSoftware.Blazor.Utilities just contains the MessagingCenter service which is the same that is implemented in Xamarin.Forms to be used in Blazor projects as a service to send the data across the components using the Publish - Subscription pattern in addition to the existing model of Parameters & Event Callbacks.

Note: The messaging center class with the current package is the same class and cloned from the Xamarin.Forms repository from the following link Xamarin.Forms

Check out the sample project:

In the current repository you can find a sample project to use the MessagingCenter service in Blazor that shows how to send a string value from one component to two other components without a direct relationship between them and update their states. The example shows using updating the username in a form component then using MessagingCenter we can send that update to the NavMenu component and the MainLayout and update the value there. Check out the following folder:

🌎 πŸ‘· Get Started with Blazor MessagingCenter β›³

Make sure to instal the NuGet package of the library using the NuGet command

	Install-Package AKSoftware.Blazor.Utilities

Or through the .NET CLI

	dotnet package install AKSoftware.Blazor.Utilities

Now in the _imports.razor and add the following namesapce

@using AKSoftware.Blazor.Utilities

⚑ πŸš€ Publish a message πŸš€ ⚑

From the component that you want to send data from example a sample string like the example in the repository

The code takes a the sender object as a parameter, the filter of the message like for this example "greeting_message" which will be used by other components to receive the message, and the last parameter is the value to be sent

public void SendMessage()
{
     string valueToSend = "Hi from Component 1";
     MessagingCenter.Send(this, "greeting_message", valueToSend);
}

✈️ Receive a message from destination component ✈️

In the destination component all you can do is calling the subscribe method to subscribe for a target of messaging

Blazor WebAssembly

public void SubscribeToMessage()
{
	MessagingCenter.Subscribe<Component1, string>(this, "greeting_message", (sender, value) => 
	{
		// Do actions against the value 
		// If the value is updating the component make sure to call 
		string greeting = $"Welcome {value}";
		StateHasChanged(); // To update the state of the component 
	});
}

🚧⚠️ Blazor Server Notice 🚧⚠️

Becuase Blazor Server working on thread make sure to call that InvokeAsync within the callback Note: This will be resolved and updated in the version 1.0.1 Special thanks for Ferenc Czirok for mentioning the note and the following code snippet

public void SubscribeToMessage()
{
	MessagingCenter.Subscribe<Component1, string>(this, "greeting_message", async (sender, value) => 
	{
		// Do actions against the value 
		// If the value is updating the component make sure to call 
		// Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state
		await InvokeAsync(() => {
			string greeting = $"Welcome {value}";
			StateHasChanged(); // To update the state of the component 
		});
	});
}

Enjoy it 🎒

🚧 How to contribute

Contributions are welcomed to the current repository you can get started by

⚠️ You have problems???

Feel free to open any issue directly from the issues section here GitHub Issues

Developed with all ❀️ by πŸ’» Ahmad Mozaffar

About

Messaging is a repository that represents the MessagingCenter service available in Xamarin as a package available for all Blazor developers that allows them to send data between components efficiently

License:MIT License


Languages

Language:C# 52.2%Language:HTML 30.0%Language:CSS 17.8%