Skip to content

Commit 8cbc9ec

Browse files
authored
Merge pull request #360 from MicrosoftDocs/mcleans/xamlisland-winui
Updated XAML Island instructions
2 parents 06bb878 + 0ae36e0 commit 8cbc9ec

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

hub/apps/desktop/modernize/host-custom-control-with-xaml-islands.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.custom: 19H1
1212

1313
# Host a custom UWP control in a WPF app using XAML Islands
1414

15-
This article demonstrates how to use the [WindowsXamlHost](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost) control in the Windows Community Toolkit to host a custom UWP control in a WPF app that targets .NET Core 3. The custom control contains several first-party UWP controls and binds a property in one of the UWP controls to a string in the WPF app.
15+
This article demonstrates how to use the [WindowsXamlHost](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost) control in the Windows Community Toolkit to host a custom UWP control in a WPF app that targets .NET Core 3. The custom control contains several first-party UWP controls from the Windows SDK and binds a property in one of the UWP controls to a string in the WPF app. This article also demonstrates how to also host a first-party UWP control from the [WinUI library](https://docs.microsoft.com/uwp/toolkits/winui/).
1616

1717
Although this article demonstrates how to do this in a WPF app, the process is similar for a Windows Forms app. For an overview about hosting UWP controls in WPF and Windows Forms apps, see [this article](xaml-islands.md#wpf-and-windows-forms-applications).
1818

@@ -101,7 +101,7 @@ Next, add a UWP app project to the same solution as your WPF project. You will r
101101

102102
To host a custom UWP control in your WPF app, you must have the source code for the control so you can compile it with your app. Typically custom controls are defined in a UWP class library project for easy portability.
103103

104-
In this section, you will define a simple custom UWP control in a new class library project. You can alternatively define the custom UWP control in the UWP app project you created in the previous section. However, these steps do this in a separate class library project for illustrative purposes because this is typically how custom controls are implemented for portability.
104+
In this section, you will define a simple custom UWP control in a new class library project. You can alternatively define the custom UWP control in the UWP app project you created in the previous section. However, these steps do this in a separate class library project for illustrative purposes because this is typically how custom controls are implemented for portability.
105105

106106
If you already have a custom control, you can use it instead of the control shown here. However, you'll still need to configure the project that contains the control as shown in these steps.
107107

@@ -191,6 +191,60 @@ If you already have a custom control, you can use it instead of the control show
191191

192192
6. Build and run your app and confirm that the UWP user control displays as expected.
193193

194+
## Add a control from the WinUI library to the custom control
195+
196+
Traditionally, UWP controls have been released as part of the Windows 10 OS and made available to developers through the Windows SDK. The [WinUI library](https://docs.microsoft.com/uwp/toolkits/winui/) is an alternative approach, where updated versions of the first-party UWP controls from the Windows SDK are distributed in a NuGet package that is not tied to Windows SDK releases. This library also includes new controls that aren't part of the Windows SDK and the default UWP platform. See our [WinUI library roadmap](https://github.com/microsoft/microsoft-ui-xaml/blob/master/docs/roadmap.md) for more details.
197+
198+
This section demonstrates how to add a UWP control from the WinUI library to your user control so you can host this control in your WPF app.
199+
200+
1. In the UWP app project, install the latest prerelease version of the [Microsoft.UI.Xaml](https://www.nuget.org/packages/Microsoft.UI.Xaml) NuGet package.
201+
> [!NOTE]
202+
> Make sure you install the latest *prerelease* version. Currently, only prerelease versions of this package will work if you choose to package your app in an [MSIX package](https://docs.microsoft.com/windows/msix) for deployment.
203+
204+
2. In the App.xaml file in this project, add the following child element to the `<xaml:Application>` element.
205+
206+
```xml
207+
<Application.Resources>
208+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
209+
</Application.Resources>
210+
```
211+
212+
After adding this element, the contents of this file should now look similar to this.
213+
214+
```xml
215+
<xaml:XamlApplication
216+
x:Class="MyUWPApp.App"
217+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
218+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
219+
xmlns:xaml="using:Microsoft.Toolkit.Win32.UI.XamlHost"
220+
xmlns:local="using:MyUWPApp">
221+
<Application.Resources>
222+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
223+
</Application.Resources>
224+
</xaml:XamlApplication>
225+
```
226+
227+
3. In the UWP class library project, install the latest prerelease version of the [Microsoft.UI.Xaml](https://www.nuget.org/packages/Microsoft.UI.Xaml) NuGet package (the same version that you installed in the UWP app project).
228+
229+
4. In the same project, open the XAML file for the user control and add the following namespace declaration to the `<UserControl>` element.
230+
231+
```xml
232+
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
233+
```
234+
235+
5. In the same file, add a `<winui:RatingControl />` element as a child of the `<StackPanel>`. This element adds an instance of the [RatingControl](https://docs.microsoft.com/uwp/api/microsoft.ui.xaml.controls.ratingcontrol?view=winui-2.2) class from the WinUI library. After adding this element, the `<StackPanel>` should now look similar to this.
236+
237+
```xml
238+
<StackPanel Background="LightCoral">
239+
<TextBlock>This is a simple custom UWP control</TextBlock>
240+
<Rectangle Fill="Blue" Height="100" Width="100"/>
241+
<TextBlock Text="{x:Bind XamlIslandMessage}" FontSize="50"></TextBlock>
242+
<winui:RatingControl />
243+
</StackPanel>
244+
```
245+
246+
6. Build and run your app and confirm that the new rating control displays as expected.
247+
194248
## Package the app
195249

196250
You can optionally package the WPF app in an [MSIX package](https://docs.microsoft.com/windows/msix) for deployment. MSIX is the modern app packaging technology for Windows, and it is based on a combination of MSI, APPX, App-V and ClickOnce installation technologies.

hub/apps/desktop/modernize/host-standard-control-with-xaml-islands.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@ ms.custom: 19H1
1515
This article demonstrates two ways to host a standard UWP control (that is, a first-party UWP control provided by the Windows SDK or WinUI library) in a WPF app by using [XAML Islands](xaml-islands.md):
1616

1717
* It shows how to host a UWP [InkCanvas](https://docs.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.InkCanvas) and [InkToolbar](https://docs.microsoft.com/uwp/api/windows.ui.xaml.controls.inktoolbar) controls by using [wrapped controls](xaml-islands.md#wrapped-controls) in the Windows Community Toolkit. These controls wrap the interface and functionality of a small set of useful UWP controls. You can add them directly to the design surface of your WPF or Windows Forms project and then use them like any other WPF or Windows Forms control in the designer.
18+
1819
* It also shows how to host a UWP [CalendarView](https://docs.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.CalendarView) control by using the [WindowsXamlHost](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost) control in the Windows Community Toolkit. Because only a small set of UWP controls are available as wrapped controls, you can use [WindowsXamlHost](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost) to host any other standard UWP control.
1920

20-
Although this article demonstrates how to host these controls in a WPF app, the process is similar for a Windows Forms app.
21+
To host a UWP control in a WPF app, you'll need the following components. This article provides instructions for creating each of these components.
22+
23+
* The project and source code for your WPF app.
24+
* A UWP app project that defines an instance of the `Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication` class from the Windows Community Toolkit.
25+
> [!NOTE]
26+
> To ensure that your app works well in all XAML Island scenarios, your WPF (or Windows Forms) project must have access to a `XamlApplication` object. This object acts as a root metadata provider for loading metadata for UWP XAML types in assemblies in the current directory of your application. The recommended way to do this is to add a **Blank App (Universal Windows)** project to the same solution as your WPF (or Windows Forms) project and revise the default `App` class in this project to derive from `XamlApplication`.
27+
>
28+
> Although this step isn't required for trivial XAML Island scenarios such as hosting a first-party UWP control, your WPF app needs this `XamlApplication` object to support the full range of XAML Island scenarios, including hosting custom UWP controls. We recommend that you always add a UWP project and define a `XamlApplication` object in any solution in which you are using XAML Islands. Your solution can contain only one project that defines a `XamlApplication` object. All custom UWP controls in your app share the same `XamlApplication` object.
29+
30+
Although this article demonstrates how to host UWP controls in a WPF app, the process is similar for a Windows Forms app.
2131

2232
## Create a WPF project
2333

2434
Before getting started, follow these instructions to create a WPF project and configure it to host XAML Islands. If you have an existing WPF project, you can adapt these steps and code examples for your project.
2535

26-
1. In Visual Studio 2019, create a new **WPF App (.NET Framework)** or **WPF App (.NET Core)** project.
36+
1. In Visual Studio 2019, create a new **WPF App (.NET Framework)** or **WPF App (.NET Core)** project. If you want to create a **WPF App (.NET Core)** project, you must first install the latest available preview version of the [.NET Core 3 Preview SDK](https://dotnet.microsoft.com/download/dotnet-core/3.0).
2737

2838
2. Make sure [package references](https://docs.microsoft.com/nuget/consume-packages/package-references-in-project-files) are enabled:
2939

@@ -38,6 +48,51 @@ Before getting started, follow these instructions to create a WPF project and co
3848
> [!NOTE]
3949
> Windows Forms apps must use the [Microsoft.Toolkit.Forms.UI.Controls](https://www.nuget.org/packages/Microsoft.Toolkit.Forms.UI.Controls) package (version v6.0.0-preview7 or later).
4050
51+
6. Configure your solution to target a specific platform such as x86 or x64. Most XAML Islands scenarios are not supported in projects that target **Any CPU**.
52+
53+
1. In **Solution Explorer**, right-click the solution node and select **Properties** -> **Configuration Properties** -> **Configuration Manager**.
54+
2. Under **Active solution platform**, select **New**.
55+
3. In the **New Solution Platform** dialog, select **x64** or **x86** and press **OK**.
56+
4. Close the open dialog boxes.
57+
58+
## Create a XamlApplication object in a UWP app project
59+
60+
Next, add a UWP app project to the same solution as your WPF project. You will revise the default `App` class in this project to derive from the `Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication` class provided by the Windows Community Toolkit. Although this step isn't required for trivial XAML Island scenarios such as hosting a single first-party UWP control, your WPF app needs this `XamlApplication` object to support the full range of XAML Island scenarios. We recommend that you always add this project to any solution in which you are using XAML Islands.
61+
62+
1. In **Solution Explorer**, right-click the solution node and select **Add** -> **New Project**.
63+
2. Add a **Blank App (Universal Windows)** project to your solution. Make sure the target version and minimum version are both set to **Windows 10, version 1903** or later.
64+
3. In the UWP app project, install the [Microsoft.Toolkit.Win32.UI.XamlApplication](https://www.nuget.org/packages/Microsoft.Toolkit.Win32.UI.XamlApplication) NuGet package (version v6.0.0-preview7 or later).
65+
4. Open the **App.xaml** file and replace the contents of this file with the following XAML. Replace `MyUWPApp` with the namespace of your UWP app project.
66+
67+
```xml
68+
<xaml:XamlApplication
69+
x:Class="MyUWPApp.App"
70+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
71+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
72+
xmlns:xaml="using:Microsoft.Toolkit.Win32.UI.XamlHost"
73+
xmlns:local="using:MyUWPApp">
74+
</xaml:XamlApplication>
75+
```
76+
77+
5. Open the **App.xaml.cs** file and replace the contents of this file with the following code. Replace `MyUWPApp` with the namespace of your UWP app project.
78+
79+
```csharp
80+
namespace MyUWPApp
81+
{
82+
sealed partial class App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication
83+
{
84+
public App()
85+
{
86+
this.Initialize();
87+
}
88+
}
89+
}
90+
```
91+
92+
6. Delete the **MainPage.xaml** file from the UWP app project.
93+
7. Build the UWP app project.
94+
8. In your WPF project, right-click the **Dependencies** node and add a reference to your UWP app project.
95+
4196
## Host an InkCanvas and InkToolbar by using wrapped controls
4297

4398
Now that you have configured your project to use UWP XAML Islands, you are now ready to add the [InkCanvas](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/inkcanvas) and [InkToolbar](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/inktoolbar) wrapped UWP controls to the app.

hub/apps/desktop/modernize/modernize-wpf-tutorial-2.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ This is the second part of a tutorial that demonstrates how to modernize a sampl
1616

1717
In the fictional scenario of this tutorial, the Contoso development team wants to add support for digital signatures to the Contoso Expenses app. The UWP **InkCanvas** control is a great option for this scenario, because it supports digital ink and AI-powered features like the capability to recognize text and shapes. To do this, you will use the [InkCanvas](https://docs.microsoft.com/windows/communitytoolkit/controls/wpf-winforms/inkcanvas) wrapped UWP control available in the Windows Community Toolkit. This control wraps the interface and functionality of the UWP **InkCanvas** control for use in a WPF app. For more details about wrapped UWP controls, see [Host UWP XAML controls in desktop apps (XAML Islands)](xaml-islands.md).
1818

19+
> [!NOTE]
20+
> In this tutorial, the WPF app will host only first-party UWP controls from the Windows SDK. To support other XAML Island scenarios including custom UWP controls, the app project must have access to an instance of the `Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication` class provided by the Windows Community Toolkit. The recommended way to do this is to add a **Blank App (Universal Windows)** project to the same solution as your WPF (or Windows Forms) project and revise the default `App` class in this project. Because this step isn't required for the basic scenario of hosting first-party UWP controls from the Windows SDK, this tutorial omits this step. For more details, see [this article](host-standard-control-with-xaml-islands.md).
21+
1922
## Configure the project to use XAML Islands
2023

2124
Before you can add an **InkCanvas** control to the Contoso Expenses app, you first need to configure the project to support UWP XAML Islands.

0 commit comments

Comments
 (0)