Skip to content

Commit de947b6

Browse files
committed
Winforms Xaml Island sample: Add a WinUI Class Library
1 parent 8c29ea6 commit de947b6

File tree

11 files changed

+267
-9
lines changed

11 files changed

+267
-9
lines changed

Samples/Islands/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ This sample is an "unpackaged" app, so it will run like a WinForms/Win32 app doe
102102

103103
This sample uses Windows App SDK as a "framework package". This means that the Windows App SDK runtime must be installed for it to run.
104104

105+
It also shows how to use a UserControl named "SampleUserControl" that's defined in a separate "WinUI Class Library" DLL called
106+
"SampleWinUIClassLibrary". To make this work, the XamlApp type knows how to route Xaml metadata requests to the SampleWinUIClassLibrary's
107+
XamlMetadataProvider object.
108+
105109
## Prerequisites
106110

107111
* See [System requirements for Windows app development](https://docs.microsoft.com/windows/apps/windows-app-sdk/system-requirements).
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="MyMessage.Text" xml:space="preserve">
121+
<value>Hello from the class library!</value>
122+
</data>
123+
</root>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<UserControl
3+
x:Class="SampleWinUIClassLibrary.SampleUserControl"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:SampleWinUIClassLibrary"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
11+
<Grid BorderBrush="Gray" BorderThickness="1" Background="#1199ff99" CornerRadius="5">
12+
<StackPanel Margin="5">
13+
<TextBlock FontSize="18">SampleUserControl</TextBlock>
14+
<TextBlock x:Uid="MyMessage"></TextBlock>
15+
</StackPanel>
16+
</Grid>
17+
</UserControl>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using Microsoft.UI.Xaml.Controls.Primitives;
4+
using Microsoft.UI.Xaml.Data;
5+
using Microsoft.UI.Xaml.Input;
6+
using Microsoft.UI.Xaml.Media;
7+
using Microsoft.UI.Xaml.Navigation;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.IO;
11+
using System.Linq;
12+
using System.Runtime.InteropServices.WindowsRuntime;
13+
using Windows.Foundation;
14+
using Windows.Foundation.Collections;
15+
16+
// To learn more about WinUI, the WinUI project structure,
17+
// and more about our project templates, see: http://aka.ms/winui-project-info.
18+
19+
namespace SampleWinUIClassLibrary
20+
{
21+
public sealed partial class SampleUserControl : UserControl
22+
{
23+
public SampleUserControl()
24+
{
25+
InitializeComponent();
26+
}
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
4+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
5+
<RootNamespace>SampleWinUIClassLibrary</RootNamespace>
6+
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
7+
<UseWinUI>true</UseWinUI>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<None Remove="SampleUserControl.xaml" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4948" />
14+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.250907003" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<Page Update="SampleUserControl.xaml">
18+
<Generator>MSBuild:Compile</Generator>
19+
</Page>
20+
</ItemGroup>
21+
22+
</Project>

Samples/Islands/cs-winforms-unpackaged/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ static void Main()
1717
// Island-support: This is necessary for Xaml controls, resources, and metatdata to work correctly.
1818
var xamlApp = new XamlApp();
1919

20+
// Island-support: We need to add the SampleWinUIClassLibrary's XamlMetaDataProvider so that the Xaml parser
21+
// can find the Xaml controls in the SampleWinUIClassLibrary (like "SampleUserControl").
22+
// Without this, you will get a XamlParseException.
23+
xamlApp.AddProvider(new SampleWinUIClassLibrary.SampleWinUIClassLibrary_XamlTypeInfo.XamlMetaDataProvider());
24+
2025
// Island-support: This allows the WindowsAppSDK UI stack to process messages before the WinForms message loop.
2126
WindowsAppSdkHelper.EnableContentPreTranslateMessageInEventLoop();
2227

Samples/Islands/cs-winforms-unpackaged/WinFormsWithIslandApp.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
<ItemGroup>
2929
<!-- Island-support: Reference the WindowsAppSDK and packages it depends on -->
30-
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250513003" />
30+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.250907003" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\SampleWinUIClassLibrary\SampleWinUIClassLibrary.csproj" />
3135
</ItemGroup>
3236

3337
</Project>

Samples/Islands/cs-winforms-unpackaged/WinFormsWithIslandApp.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,52 @@ VisualStudioVersion = 17.11.35303.130
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsWithIslandApp", "WinFormsWithIslandApp.csproj", "{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWinUIClassLibrary", "..\SampleWinUIClassLibrary\SampleWinUIClassLibrary.csproj", "{C404993A-B18A-48DF-B87C-F37B77F9E6AB}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
1013
Debug|ARM64 = Debug|ARM64
1114
Debug|x64 = Debug|x64
1215
Debug|x86 = Debug|x86
16+
Release|Any CPU = Release|Any CPU
1317
Release|ARM64 = Release|ARM64
1418
Release|x64 = Release|x64
1519
Release|x86 = Release|x86
1620
EndGlobalSection
1721
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|Any CPU.ActiveCfg = Debug|x64
23+
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|Any CPU.Build.0 = Debug|x64
1824
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|ARM64.ActiveCfg = Debug|ARM64
1925
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|ARM64.Build.0 = Debug|ARM64
2026
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|x64.ActiveCfg = Debug|x64
2127
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|x64.Build.0 = Debug|x64
2228
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|x86.ActiveCfg = Debug|x86
2329
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Debug|x86.Build.0 = Debug|x86
30+
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|Any CPU.ActiveCfg = Release|x64
31+
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|Any CPU.Build.0 = Release|x64
2432
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|ARM64.ActiveCfg = Release|ARM64
2533
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|ARM64.Build.0 = Release|ARM64
2634
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|x64.ActiveCfg = Release|x64
2735
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|x64.Build.0 = Release|x64
2836
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|x86.ActiveCfg = Release|x86
2937
{1E4FD1DC-BC9D-491F-B7AE-D1AE9802E1FE}.Release|x86.Build.0 = Release|x86
38+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|ARM64.ActiveCfg = Debug|Any CPU
41+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|ARM64.Build.0 = Debug|Any CPU
42+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|x64.ActiveCfg = Debug|Any CPU
43+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|x64.Build.0 = Debug|Any CPU
44+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|x86.ActiveCfg = Debug|Any CPU
45+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Debug|x86.Build.0 = Debug|Any CPU
46+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|ARM64.ActiveCfg = Release|Any CPU
49+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|ARM64.Build.0 = Release|Any CPU
50+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|x64.ActiveCfg = Release|Any CPU
51+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|x64.Build.0 = Release|Any CPU
52+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|x86.ActiveCfg = Release|Any CPU
53+
{C404993A-B18A-48DF-B87C-F37B77F9E6AB}.Release|x86.Build.0 = Release|Any CPU
3054
EndGlobalSection
3155
GlobalSection(SolutionProperties) = preSolution
3256
HideSolutionNode = FALSE

Samples/Islands/cs-winforms-unpackaged/WinUI3Page.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d"
10+
xmlns:sample="using:SampleWinUIClassLibrary"
1011
>
1112
<StackPanel Margin="10">
1213
<Button x:Name="MyButton" Click="Button_Click">I am a WinUI 3 Button</Button>
1314
<TextBox Text="I am a WinUI 3 TextBox" />
1415
<ComboBox ItemsSource="{x:Bind MyItems}" />
16+
<sample:SampleUserControl />
1517
</StackPanel>
1618
</Page>
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.UI.Xaml.Hosting;
1+
using Microsoft.UI.Xaml.Controls;
2+
using Microsoft.UI.Xaml.Hosting;
23
using Microsoft.UI.Xaml.Markup;
4+
using Microsoft.UI.Xaml.XamlTypeInfo;
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
@@ -15,31 +17,58 @@ internal class XamlApp : Microsoft.UI.Xaml.Application, IXamlMetadataProvider
1517
{
1618
public XamlApp()
1719
{
18-
_xamlMetaDataProvider = new Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider();
20+
AddProvider(new XamlControlsXamlMetaDataProvider());
1921
_windowsXamlManager = WindowsXamlManager.InitializeForCurrentThread();
2022
}
2123

24+
public void AddProvider(IXamlMetadataProvider provider)
25+
{
26+
_providers.Add(provider);
27+
}
28+
2229
override protected void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
2330
{
2431
this.Resources.MergedDictionaries.Add(new Microsoft.UI.Xaml.Controls.XamlControlsResources());
2532
}
2633

27-
IXamlType IXamlMetadataProvider.GetXamlType(string fullName)
34+
IXamlType? IXamlMetadataProvider.GetXamlType(string fullName)
2835
{
29-
return _xamlMetaDataProvider.GetXamlType(fullName);
36+
foreach (var provider in _providers)
37+
{
38+
var xamlType = provider.GetXamlType(fullName);
39+
if (xamlType != null)
40+
{
41+
return xamlType;
42+
}
43+
}
44+
return null;
3045
}
3146

32-
IXamlType IXamlMetadataProvider.GetXamlType(System.Type type)
47+
IXamlType? IXamlMetadataProvider.GetXamlType(System.Type type)
3348
{
34-
return _xamlMetaDataProvider.GetXamlType(type);
49+
foreach (var provider in _providers)
50+
{
51+
var xamlType = provider.GetXamlType(type);
52+
if (xamlType != null)
53+
{
54+
return xamlType;
55+
}
56+
}
57+
return null;
3558
}
3659

3760
XmlnsDefinition[] IXamlMetadataProvider.GetXmlnsDefinitions()
3861
{
39-
return _xamlMetaDataProvider.GetXmlnsDefinitions();
62+
var definitions = new List<XmlnsDefinition>();
63+
foreach (var provider in _providers)
64+
{
65+
var providerDefinitions = provider.GetXmlnsDefinitions();
66+
definitions.AddRange(providerDefinitions);
67+
}
68+
return definitions.ToArray();
4069
}
4170

4271
WindowsXamlManager _windowsXamlManager;
43-
IXamlMetadataProvider _xamlMetaDataProvider;
72+
List<IXamlMetadataProvider> _providers = new List<IXamlMetadataProvider>();
4473
}
4574
}

0 commit comments

Comments
 (0)