--- description: 'WinUI 3 / WinAppSDK architecture, MVVM, XAML patterns, DI, theming, and controls guidance' applyTo: '**/*.cs, **/*.xaml, **/*.csproj' --- # WinUI 3 / WinAppSDK -- Best Practices & Patterns This file covers WinUI 3-specific patterns, conventions, and architecture guidance for this project. --- ## 1. Architecture -- MVVM Pattern ### Overview Use **Model-View-ViewModel (MVVM)** for all UI features: | Layer | Responsibility | Example | |---|---|---| | **Model** | Data structures & business entities | `Item.cs`, `UserProfile.cs` | | **View** | XAML UI -- layout, styles, animations | `MainPage.xaml` | | **ViewModel** | UI state, commands, data transformation | `MainViewModel.cs` | | **Service** | Business logic, data access, navigation | `IDataService.cs`, `NavigationService.cs` | ### Project Folder Structure ``` / Models/ -> Data classes ViewModels/ -> ViewModels (one per page/dialog) Views/ -> XAML pages and windows Services/ -> Business logic & platform services Converters/ -> IValueConverter implementations Helpers/ -> Static utility methods Controls/ -> Custom/reusable controls Strings/ en-us/ Resources.resw Assets/ -> Images, icons, splash screens ``` ### ViewModel Base Use `CommunityToolkit.Mvvm` (recommended) for boilerplate-free ViewModels: ```xml ``` ```csharp using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; public partial class MainViewModel : ObservableObject { [ObservableProperty] private string _title = string.Empty; [ObservableProperty] private bool _isLoading; [RelayCommand] private async Task LoadDataAsync() { IsLoading = true; try { // Load data } finally { IsLoading = false; } } } ``` ### View-ViewModel Binding ```xml