WPF

42.1 WPF Overview

A modern Windows UI framework: XAML markup, vector graphics, styles/templates, powerful data binding, MVVM. It works with VB on .NET Framework and .NET 8+.

42.2 XAML and Code-Behind

<!-- MainWindow.xaml -->
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Task List" Height="400" Width="500">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding NewTask, UpdateSourceTrigger=PropertyChanged}" Width="300" Margin="0,0,5,0"/>
            <Button Content="Add" Command="{Binding AddCommand}" Width="80"/>
        </StackPanel>
        <ListBox Grid.Row="1" ItemsSource="{Binding Tasks}" Margin="0,10,0,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding Title}" IsChecked="{Binding IsDone}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
' MainWindow.xaml.vb
Class MainWindow
    Public Sub New()
        InitializeComponent()
        DataContext = New MainViewModel()
    End Sub
End Class

42.3 MVVM in VB

WPF concepts: layout panels (Grid, StackPanel, DockPanel, WrapPanel, Canvas), dependency properties, routed events, resources and styles, control templates, triggers, value converters (IValueConverter), animations, Dispatcher.Invoke.

The CommunityToolkit.Mvvm library works from VB (its source generators require C#, but ObservableObject/RelayCommand classes are usable directly).

Imports System.Collections.ObjectModel
Imports System.Windows.Input

Public Class TaskItem
    Inherits ViewModelBase                              ' from Module 22.6
    Private _isDone As Boolean
    Public Property Title As String
    Public Property IsDone As Boolean
        Get
            Return _isDone
        End Get
        Set(value As Boolean)
            SetProperty(_isDone, value)
        End Set
    End Property
End Class

Public Class RelayCommand
    Implements ICommand
    Private ReadOnly _execute As Action(Of Object)
    Private ReadOnly _canExecute As Func(Of Object, Boolean)
    Public Sub New(execute As Action(Of Object), Optional canExecute As Func(Of Object, Boolean) = Nothing)
        _execute = execute : _canExecute = canExecute
    End Sub
    Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
        AddHandler(value As EventHandler)
            AddHandler CommandManager.RequerySuggested, value
        End AddHandler
        RemoveHandler(value As EventHandler)
            RemoveHandler CommandManager.RequerySuggested, value
        End RemoveHandler
        RaiseEvent(sender As Object, e As EventArgs)
        End 
…

42.4 Other Desktop Options

Avalonia UI (cross-platform, XAML) can be used with VB with some setup. WinUI 3 and .NET MAUI don't support VB. Use C# for the UI layer and VB for libraries if needed.