With the first glance at Blazor - a framework for building interactive client-side web UI with .NET, I have that idea - it's perfect for MVVM.
I started with MVVM on WPF apps many years ago and spent a lot of time with MVVMLight and Xamarin, so I immediately saw an advantage of it for Blazor as well.
- Like in WPF, Blazor has the most important feature - data binding. In short, you have some data, typically in a collection of some sort, and you want to display it to the user. You can 'bind' your view to the data.
- Like in WPF, Blazor has two parts, the
.razorpage which describes your GUI in HTML, and the code-behind
.razor.csthat is tied to it.
This makes Blazor MVVM default for me. So I picked some base components from old MVVM projects which I want to share.
This is ComponentBase which I use for MVVM everywhere.
DataContext is quite basic, we only should ensure to initialize it only once for
ServerPrerendering mode because it will call
OnInitializeAsync twice due to, well, pre-rendering.
OnInitializeAsync, and data initialization in
OnAfterRender looks wrong to me (at least it's not what docs propose).
And there are things like js-loading indicator which need to be started if the page loaded but the data not.
Much confusing... For now, I simply move all JS initialization to
And how you would solve the JS problem in the MVVM pattern?