I released the Blazing.MVVM library that enables Blazor to use the CommunityToolkit.Mvvm with minimal effort. It also incldes Navigation by ViewModel (class/interface) - no more magic strings!
Whilst the repo contains a basic sample project showing how to use the library, I wanted to include a sample that takes an existing project for a different application type and, with minimal changes, make it work for Blazor. So I have taken Microsoft's Xamarin Sample Project and converted it to Blazor.
The MvvmSample.Core
project remains mostly unchanged, I've added base classes to the ViewModels to enable Blazor binding updates.
So, as an example, the SamplePageViewModel
was changed from:
public class MyPageViewModel : ObservableObject
{
// code goes here
}
to:
public class MyPageViewModel : ViewModelBase
{
// code goes here
}
The ViewModelBase
wraps the ObservableObject
class. No other changes required.
For the Xamarin Pages, wiring up the DataContext
is done with:
BindingContext = Ioc.Default.GetRequiredService<MyPageViewModel>();
With Blazing.MVVM, it is simply:
@inherits MvvmComponentBase<MyPageViewModel>
Lastly, I have updated all of the documentation used in the Sample application from Xamarin-specific to Blazor. If I have missed any changes, please let me know and I will update.
Xamarin comes with a rich set of controls. Blazor is lean in comparison. To keep this project lean, I have included my own ListBox
and Tab
controls - enjoy! When I have time, I will endeavor to complete and release a control library for Blazor.
I have added new WPF/Avalonia Hybrid apps to demonstrate Calling into Blazor from WPF/Avalonia using MVVM. To do this, I have:
- moved the core shared parts from the BlazorSample app to a new RCL (Razor Class Library)
- Moved the Assets to a standard
Content
folder as thewwwroot
is no longer accessable. TheBlazorWebView
host control uses ip address0.0.0.0
which is invalid for thehttpClient
. - Added new
FileService
class to the WPF/Avalonia app to use theFile
class and not theHttpClient
. - Added a new App.Razor to the WPF/Avalonia app for custom Blazor layout and hook the shared state for handling navigation requests from WPF/Avalonia
To enable the calling into the Blazor app, I have used a static state class to hold a reference to the NavigationManager
and MvvvmNavigationManager
classes.
I have kept the code as close to the original solution as possible. Blazor does not have a rich data binding system, so there as small variations to the original code, however no huge changes made. Most of the work is handled by the Blazing.MVVM library.
Download the project, run it, and compare with the original.
- Initial release.
- Added Wpf & Avalonia sample Blazor Hybrid Apps
- Updated to .Net 8.0
If you find this library useful, then please consider buying me a coffee ☕.