You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A command is a syntactic sugar for methods defined in the ViewModel.
It is made available as a property to enable binding.
As the ViewModel can inherit, so can commands.
This request aims to include the inheritance modifier in the command property, as outlined in the method definition.
Support for the following: abstract, virtual, override, and sealed.
This can be enabled as an explicit opt-in through a RelayCommand attribute property.
API breakdown
A Boolean property for the RelayCommand attribute
Update Command SG to infer the inheritance modifiers when the attribute above is set to true
// Base classpublicvirtualIRelayCommandInitializeCommand{get;}// Source generated// The attribute property name can be formalized[RelayCommand(Inherit=true)]// Instructing the command property to include the modifierprotectedvirtualvoidInitialize(){}// Derived classpublicoverrideIRelayCommandInitializeCommand{get;}// Source generated[RelayCommand]protectedoverridevoidInitialize(){}
Usage example
This ensures that the ViewModel types inherited from BaseViewModel can optionally override the InitializeCommand definition, if necessary.
For example: If logging is to be implemented, the base definition is sufficient for most scenarios. However, for special cases, additional logic will need to be incorporated.
namespaceMyApp.ViewModels;publicclassBaseViewModel{publicvirtualIAsyncRelayCommandInitializeCommand{get;}publicBaseViewModel(){InitializeCommand=newAsyncRelayCommand(InitializeAsync);}//[RelayCommand]protectedvirtualTaskInitializeAsync(){// Base command implementation}}publicclassProjectViewModel:BaseViewModel{publicoverrideIAsyncRelayCommandInitializeCommand{get;}publicProjectViewModel(){InitializeCommand=newAsyncRelayCommand(InitializeAsync);}//[RelayCommand]protectedoverrideTaskInitializeAsync(){// Derived command implementation// Can invoke base implementation, if necessary// base.InitializeAsync();}}
Breaking change?
No
Alternatives
Manual command infra. Tedious and cumbersome.
Additional context
There is a breaking change in .NET MAUI, Behaviors no longer automatically inherit the BindingContext of their parent.
In one of the projects, EventToCommandBehavior was mapped to invoke a Command on the Page Load event.
As I migrate to the latest version, I will move this Behavior mapping to the base definition for minimal changes, and override the Command in Derived types if additional logic is needed, instead of manually modifying the affected Pages and ViewModels.
Help us help you
No, just wanted to propose this
The text was updated successfully, but these errors were encountered:
If SG can infer the attributes of the initial virtual method definition, the RelayCommand attribute property association can be limited to it, making it even easier.
In simple terms, if a virtual method is annotated with the RelayCommand attribute with the Inherit property set to true, the override methods will inherit the modifier(s) upon further annotation; otherwise, they will not.
Overview
A command is a syntactic sugar for methods defined in the ViewModel.
It is made available as a property to enable binding.
As the ViewModel can inherit, so can commands.
This request aims to include the inheritance modifier in the command property, as outlined in the method definition.
Support for the following: abstract, virtual, override, and sealed.
This can be enabled as an explicit opt-in through a
RelayCommand
attribute property.API breakdown
Usage example
This ensures that the ViewModel types inherited from BaseViewModel can optionally override the InitializeCommand definition, if necessary.
For example: If logging is to be implemented, the base definition is sufficient for most scenarios. However, for special cases, additional logic will need to be incorporated.
Breaking change?
No
Alternatives
Manual command infra. Tedious and cumbersome.
Additional context
There is a breaking change in .NET MAUI, Behaviors no longer automatically inherit the BindingContext of their parent.
In one of the projects, EventToCommandBehavior was mapped to invoke a Command on the Page Load event.
As I migrate to the latest version, I will move this Behavior mapping to the base definition for minimal changes, and override the Command in Derived types if additional logic is needed, instead of manually modifying the affected Pages and ViewModels.
Help us help you
No, just wanted to propose this
The text was updated successfully, but these errors were encountered: