There are several ways to get your XAML controls to appear in Visual Studio's Toolbox pane. Some of the approaches are outlined below.
Population mechanism | Supported Frameworks | Supported Visual Studio Versions |
---|---|---|
Unreferenced assemblies | WPF .NET Framework | All supported versions |
Referenced NuGet packages | All XAML frameworks | Visual Studio 15.0 and later |
Unreferenced NuGet packages | WPF .NET Core | Visual Studio 16.7 Preview 2 and later |
This approach requires registering with the Toolbox Controls Installer (TCI) in the Windows registry. The TCI specification explains how to register your assemblies. Once registered, your controls will be shown in the Toolbox for any compatible project, even projects that do not reference your assembly.
- This approach is only supported for WPF .NET Framework assemblies.
- Since registration requires modifying the registry, you need to provide an installer of some sort.
If a NuGet package referenced by a XAML project contains a tools\VisualStudioToolsManifest.xml file, Toolbox will show the Toolbox items listed in the manifest.
You can try this by adding a reference to your own package or to our WPF .NET Core sample package.
If a NuGet package in a NuGet fallback folder (see Getting Started section below) contains a tools\VisualStudioToolsManifest.xml file, Toolbox will show the Toolbox items listed in the manifest for any project that is compatible with the package, even projects that do not reference that package.
- It currently only supports WPF .NET Core packages and projects.
-
Create a NuGet package containing WPF .NET Core controls and a tools\VisualStudioToolsManifest.xml file. Note that a UIFramework="WPF" attribute is required on ToolboxItems nodes on Visual Studio 16.7 Preview 3 or above. You could create your own package, or build our sample package.
-
Create a new directory somewhere on disk to serve as your NuGet fallback folder.
-
Create a new text file with the extension .config (ex. MyFallbackFolder.config) in %ProgramFiles(x86)%\NuGet\Config.
-
Add the following XML to the .config file:
<?xml version="1.0" encoding="utf-8"?> <configuration> <fallbackPackageFolders> <add key="My Unique Key" value="c:\MyFallbackFolder" /> </fallbackPackageFolders> </configuration>
-
Replace "c:\MyFallbackFolder" with the actual path to your fallback folder.
-
Replace "My Unique Key" with some value that is unlikely to be used by other SDKs. Note that if two NuGet .config files declare fallbackPackageFolders with the same key attribute value, one of them will win and the other will be ignored. To avoid unexpected interactions between .config files, be they from different control vendors or different SDKs from the same vendor, you should pick key values that are unlikely to be used by others. For example, consider combining your company and SDK names.
-
Download nuget.exe as described here.
-
Expand your NuGet package into your fallback folder using the following command. It's important to use nuget.exe rather than simply unzipping your nupkg because nuget.exe will create a .nupkg.metadata file which is required for your package to be found during NuGet restore. When creating an installer for your SDK we recommend that you run nuget.exe on your build/dev machine and include the resulting expanded package layout in your installer, rather than running nuget.exe on the customer's machine.
nuget.exe add <path to your nupkg> -Source <path to your fallback folder> -Expand
-
Launch Visual Studio 16.7 Preview 2 or later.
-
Create a new WPF .NET Core project.
-
Open the Toolbox pane
-
Wait for both the Toolbox and XAML designer to finish initializing.
-
The Toolbox items defined in tools\VisualStudioToolsManifest.xml should appear in the Toolbox. If you're using our sample project, you should see a CustomControlLibrary.WpfCore tab in Toolbox containing a Toolbox item for the CustomButton control.
-
Double-click one of the Toolbox items to add a control of that type to the XAML file and add a package reference for the NuGet package to the project.
Toolbox population supports multiple VisualStudioToolsManifest.xml files per package -- the manifest in the tools root plus additional manifests for specific Target Framework Monikers (TFMs) in subdirectories of tools. The Toolbox will show the items from the manifest that best matches the target framework of the current project, falling back to the manifest in the tools root if there is no better match. The manifest in the tools root is also required for compatibility with older versions of Visual Studio.
For referenced NuGet packages, this feature requires Visual Studio 16.6 or later. For unreferenced NuGet packages, this feature requires Visual Studio 16.8 Preview 3 or later.
Here's an example:
File path | Will be used for projects targeting... |
---|---|
tools\net47\VisualStudioToolsManifest.xml | .NET Framework >= 4.7 |
tools\netcoreapp31\VisualStudioToolsManifest.xml | .NET Core >= 3.1 |
tools\VisualStudioToolsManifest.xml | .NET Framework < 4.7, .NET Core < 3.1, and other frameworks |
- Clone this repo.
- Open ...\samples\CustomControlLibrary.WpfCore\CustomControlLibrary.WpfCore.sln in Visual Studio.
- Build the solution.
- Generate the package (bin\Debug\CustomControlLibrary.WpfCore.1.0.0.nupkg) by right-clicking on the CustomControlLibrary.WpfCore project in Solution Explorer and selecting Pack, or by running the following command from the directory containing CustomControlLibrary.WpfCore.csproj: msbuild /t:Pack