IridiumIO.MVVM.VBSourceGenerators
0.6.1
dotnet add package IridiumIO.MVVM.VBSourceGenerators --version 0.6.1
NuGet\Install-Package IridiumIO.MVVM.VBSourceGenerators -Version 0.6.1
<PackageReference Include="IridiumIO.MVVM.VBSourceGenerators" Version="0.6.1" />
<PackageVersion Include="IridiumIO.MVVM.VBSourceGenerators" Version="0.6.1" />
<PackageReference Include="IridiumIO.MVVM.VBSourceGenerators" />
paket add IridiumIO.MVVM.VBSourceGenerators --version 0.6.1
#r "nuget: IridiumIO.MVVM.VBSourceGenerators, 0.6.1"
#:package IridiumIO.MVVM.VBSourceGenerators@0.6.1
#addin nuget:?package=IridiumIO.MVVM.VBSourceGenerators&version=0.6.1
#tool nuget:?package=IridiumIO.MVVM.VBSourceGenerators&version=0.6.1
MVVM.VBSourceGenerators
The CommunityToolkit.MVVM source generators only work in C#. This package augments the toolkit to allow some of the generators to work for VB.NET.
Working
<ObservableProperty>
- Generates a public property with OnPropertyChanged notification as well as most helper methods defined as per the CommunityToolkit.
<NotifyPropertyChangedFor(NameOf(T))>
- Generates a property with change notification for existing properties.
<RelayCommand>
- Can decorate
Sub
,Async Sub
, orAsync Function
- Supports
CanExecute
callback functionality - Supports parameter passthrough
- Supports
CancellationToken
for async commands - Supports all
RelayCommand
attribute properties from the CommunityToolkit.MVVM library, except for the CanExecute property- CanExecute is defined by simply naming a Sub
Can[MethodName]
.
- CanExecute is defined by simply naming a Sub
- Can decorate
NotifyCanExecuteChangedFor(NameOf(T))
- Use on an
ObservableProperty
marked field to automatically notify theCanExecute
state of a command when the property changes.
- Use on an
NotifyPropertyChangedRecipients
- Use on an
ObservableProperty
marked field to send a message when the property changes - Does not support use on existing properties, only on generated properties.
- Use on an
AttachAttribute
- Custom implementation of the
[property: Attribute]
C# usage for VB.NET - Passthrough attributes to the generated property by annotating the field/method with e.g
<AttachAttribute(GetType(JsonIgnoreAttribute))>
. - If an attached attribute requires a parameter, it must be passed into AttachAttribute as a fully quality string instead.
- Custom implementation of the
Planned
Installation
- Add the NuGet package for MVVM.VBSourceGenerators to your VB.NET project:
dotnet add package IridiumIO.MVVM.VBSourceGenerators
- Ensure that you also reference
CommunityToolkit.MVVM
in your project.
ObservableProperty Usage
- In your VB.NET ViewModel, use the
[ObservableProperty]
attribute as you would in C# - Make sure your fields follow the naming convention of starting with an underscore (e.g.,
_name
). - You can then use the generated Uppercase property
Name
in your code.
Example 1:
Imports CommunityToolkit.Mvvm.ComponentModel
Partial Public Class MyViewModel
Inherits ObservableObject
<ObservableProperty>
Private _name As String
End Class
Example 2: Including NotifyPropertyChangedFor
Imports CommunityToolkit.Mvvm.ComponentModel
Partial Public Class PersonViewModel
Inherits ObservableObject
<ObservableProperty>
<NotifyPropertyChangedFor(NameOf(FullName))>
Private _firstName As String
<ObservableProperty>
<NotifyPropertyChangedFor(NameOf(FullName))>
Private _lastName As String
Public ReadOnly Property FullName As String
Get
Return $"{FirstName} {LastName}"
End Get
End Property
End Class
Additional Features:
- As defined here, additional methods for On[Name]Changed and On[Name]Changing are also generated as partial methods and can be included.
RelayCommand Usage
- In your VB.NET ViewModel, use the
[RelayCommand]
attribute as you would in C# on a method.
Examples:
Imports CommunityToolkit.Mvvm.ComponentModel
Partial Public Class MyViewModel
Inherits ObservableObject
<RelayCommand>
Private Sub Save()
' Save logic here
End Sub
<RelayCommand>
Private Sub SelectItem(item As String)
' Logic to select an item
End Sub
'Will generate two commands:
'LoadDataAsyncCommand and CancelLoadDataAsyncCommand
<RelayCommand(IncludeCancelCommand:=True)>
Private Async Function LoadDataAsync(ctx As CancellationToken) As Task
' Load data logic here
Await Task.Delay(1000) ' Simulating async work
End Function
End Class
Example 2: Including CanExecute callback
By creating a function that returns a Boolean
and prefixing it with Can
, you can control whether the command can execute.
Imports CommunityToolkit.Mvvm.ComponentModel
Partial Public Class PersonViewModel
Inherits ObservableObject
<RelayCommand>
Private Sub SaveData()
' Logic to save data
End Sub
Private Function CanSaveData() As Boolean
' Logic to determine if saving is allowed
Return MyData.Count > 0
End Function
End Class
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add support for passing-through attributes to generated properties and relaycommands
Note: V0.6.1 fixes a bug in v0.6.0 that caused WPF internally generated files to fail to compile.
- Also removes transient Microsoft.CodeAnalysis.VisualBasic dependency
- Normalised generated filenames to include namespaces when required to avoid collisions with nested types sharing the same name