implementing-communitytoolkit-mvvm
0
总安装量
1
周安装量
安装命令
npx skills add https://github.com/christian289/dotnet-with-claudecode --skill implementing-communitytoolkit-mvvm
Agent 安装分布
amp
1
cline
1
opencode
1
cursor
1
continue
1
kimi-cli
1
Skill 文档
CommunityToolkit.Mvvm Code Guidelines
A guide for implementing MVVM pattern using CommunityToolkit.Mvvm in WPF.
Project Structure
The templates folder contains a WPF project example (use latest .NET per version mapping).
templates/
âââ WpfMvvmSample.App/ â WPF Application Project
â âââ Views/
â â âââ MainWindow.xaml
â â âââ MainWindow.xaml.cs
â âââ App.xaml
â âââ App.xaml.cs
â âââ GlobalUsings.cs
â âââ WpfMvvmSample.App.csproj
âââ WpfMvvmSample.ViewModels/ â ViewModel Class Library (UI framework independent)
âââ UserViewModel.cs
âââ GlobalUsings.cs
âââ WpfMvvmSample.ViewModels.csproj
Basic Principle
Use CommunityToolkit.Mvvm as the default for MVVM structure
ObservableProperty Attribute Writing Rules
Single Attribute – Write Inline
// â
Good: Single attribute written inline
[ObservableProperty] private string _userName = string.Empty;
[ObservableProperty] private int _age;
[ObservableProperty] private bool _isActive;
Multiple Attributes – ObservableProperty Always Inline
// â
Good: Multiple attributes, ObservableProperty always inline
[NotifyPropertyChangedRecipients]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
[ObservableProperty] private string _email = string.Empty;
[NotifyDataErrorInfo]
[Required(ErrorMessage = "Name is required.")]
[MinLength(2, ErrorMessage = "Name must be at least 2 characters.")]
[ObservableProperty] private string _name = string.Empty;
[NotifyPropertyChangedRecipients]
[NotifyCanExecuteChangedFor(nameof(DeleteCommand))]
[NotifyCanExecuteChangedFor(nameof(UpdateCommand))]
[ObservableProperty] private User? _selectedUser;
Bad Example
// â Bad: ObservableProperty on separate line
[NotifyPropertyChangedRecipients]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
[ObservableProperty]
private string _email = string.Empty;
Complete ViewModel Example
namespace MyApp.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
public sealed partial class UserViewModel : ObservableObject
{
// Single attribute
[ObservableProperty] private string _firstName = string.Empty;
[ObservableProperty] private string _lastName = string.Empty;
[ObservableProperty] private int _age;
// Multiple attributes - ObservableProperty inline
[NotifyPropertyChangedRecipients]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
[ObservableProperty] private string _email = string.Empty;
[NotifyCanExecuteChangedFor(nameof(DeleteCommand))]
[NotifyCanExecuteChangedFor(nameof(UpdateCommand))]
[ObservableProperty] private User? _selectedUser;
[RelayCommand(CanExecute = nameof(CanSave))]
private async Task SaveAsync()
{
// Save logic
}
private bool CanSave() => !string.IsNullOrWhiteSpace(Email);
}
Key Rules
- Single Attribute: Write
[ObservableProperty]inline right before the field declaration - Multiple Attributes: Write other attributes on separate lines, but
[ObservableProperty]is always inline at the end - Purpose: Improve code readability and maintain consistent coding style