|
| 1 | +--- |
| 2 | +title: Data Binding with WinUI and MVVM Toolkit - Step 1 - Create a class library project |
| 3 | +description: Create a separate class library project to hold ViewModels and services for improved testability in your WinUI app. |
| 4 | +ms.date: 10/29/2025 |
| 5 | +ms.topic: tutorial |
| 6 | +keywords: windows 11, windows app sdk, winui, windows ui, mvvm, mvvm toolkit, dotnet |
| 7 | +ms.localizationpriority: medium |
| 8 | +--- |
| 9 | + |
| 10 | +# Create a class library project |
| 11 | + |
| 12 | +To enable unit testing of your ViewModels and services, create a separate class library project. You need this project because WinUI unit test projects can't directly reference WinUI app projects. |
| 13 | + |
| 14 | +## Understanding the WinUI Class Library template |
| 15 | + |
| 16 | +The **WinUI Class Library** project template creates a .NET managed class library (DLL) specifically designed for use with WinUI 3 desktop applications. This template is part of the Windows App SDK and provides important capabilities that a standard .NET class library doesn't include. |
| 17 | + |
| 18 | +### Key differences from a .NET Class Library |
| 19 | + |
| 20 | +The WinUI Class Library template differs from a standard .NET Class Library in several important ways: |
| 21 | + |
| 22 | +- **Windows-specific targeting**: It targets a Windows-specific framework (like `net8.0-windows10.0.19041.0`) rather than the cross-platform .NET framework, giving access to Windows APIs. |
| 23 | +- **Windows App SDK integration**: It includes references to the `Microsoft.WindowsAppSDK` and `Microsoft.Windows.SDK.BuildTools` NuGet packages, providing access to WinUI 3 and Windows App SDK APIs. |
| 24 | +- **WinUI support enabled**: The project includes `<UseWinUI>true</UseWinUI>` in its configuration, enabling WinUI-specific build tasks and XAML compilation. |
| 25 | +- **Windows runtime identifiers**: It's configured for Windows-specific runtime identifiers (win-x86, win-x64, win-arm64). |
| 26 | + |
| 27 | +### Why use a WinUI Class Library? |
| 28 | + |
| 29 | +Use the **WinUI Class Library** template instead of a regular **.NET Class Library** when your library needs to: |
| 30 | + |
| 31 | +- **Reference WinUI 3 types and controls**: The WinUI Class Library template allows you to use types from the `Microsoft.UI.Xaml` namespace and other Windows App SDK APIs in your library code. |
| 32 | +- **Include XAML resources**: If your library contains UserControls, custom controls, or other XAML resources, you need the WinUI Class Library template to properly compile and package these resources. |
| 33 | +- **Integrate with WinUI apps**: The template is configured to work seamlessly with WinUI 3 desktop apps, ensuring compatibility with the Windows App SDK runtime and deployment model. |
| 34 | +- **Support XAML markup compilation**: The template includes the necessary build tasks to compile XAML files into the library. |
| 35 | + |
| 36 | +### When to use a regular .NET Class Library |
| 37 | + |
| 38 | +Use a standard **.NET Class Library** project when your library: |
| 39 | + |
| 40 | +- Contains only pure .NET code (ViewModels, models, services, utilities) |
| 41 | +- Doesn't reference any WinUI 3 or Windows App SDK types |
| 42 | +- Doesn't include any XAML files or UI-related code |
| 43 | +- Needs to be shared across different application types (not just WinUI apps) |
| 44 | +- Targets multiple platforms (for example, .NET MAUI or ASP.NET Core) or operating systems (for example, Linux or macOS) |
| 45 | + |
| 46 | +For a tutorial on adding a .NET Class Library to your solution, see [Extend C# console app and debug in Visual Studio](/visualstudio/get-started/csharp/tutorial-console-part-2). |
| 47 | + |
| 48 | +For this tutorial, use the **WinUI Class Library** template because it allows you to reference WinUI types if needed in the future, and it's specifically designed to integrate with WinUI 3 applications. While our ViewModels and services don't currently require WinUI types, using this template provides flexibility and ensures proper integration with the Windows App SDK environment. |
| 49 | + |
| 50 | +## Create the WinUINotes.Bus project |
| 51 | + |
| 52 | +Create a new WinUI Class Library project named `WinUINotes.Bus` to hold your ViewModels, models, and services in the same solution as your WinUI app project. |
| 53 | + |
| 54 | +1. In Visual Studio, right-click the solution in **Solution Explorer**. |
| 55 | +1. Select **Add** > **New Project...**. |
| 56 | +1. Choose the **WinUI Class Library** template and select **Next**. |
| 57 | + |
| 58 | + > [!NOTE] |
| 59 | + > Make sure you select **WinUI Class Library**, not just **Class Library**. The WinUI Class Library template includes references to the Windows App SDK and WinUI 3 framework. |
| 60 | +
|
| 61 | +1. Name the project `WinUINotes.Bus` and select **Create**. |
| 62 | +1. Delete the default `Class1.cs` file. |
| 63 | + |
| 64 | +## Add project references |
| 65 | + |
| 66 | +Project references enable your WinUI app project to use the ViewModels and services defined in the class library project: |
| 67 | + |
| 68 | +1. Right-click the **WinUINotes** project and select **Add** > **Project Reference...**. |
| 69 | +1. Check the **WinUINotes.Bus** project and select **OK**. |
| 70 | + |
| 71 | +The Bus project contains your ViewModels, models, and services, so you can test them independently of the UI layer. |
| 72 | + |
| 73 | +> [!NOTE] |
| 74 | +> The term "Bus" indicates a project that acts as a communication layer or intermediary. It contains the presentation logic (ViewModels), business logic (models), and services that you can share and test independently of the UI. |
| 75 | +
|
| 76 | +> [!div class="nextstepaction"] |
| 77 | +> [Continue to step 2 - Implement MVVM with the MVVM Toolkit](mvvm-implementation.md) |
0 commit comments