Skip to content

Add wpf islands example #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Add wpf islands example #501

wants to merge 6 commits into from

Conversation

rmarinho
Copy link
Member

@rmarinho rmarinho commented Aug 4, 2025

Description

This pull request introduces a new WPF sample application (SimpleWpfXamlIslandApp) that demonstrates how to host WinUI 3 controls inside a WPF application using XAML Islands. The solution includes both the WPF host app and a supporting WinUI library (WinUILib) with custom controls and styles. The most important changes are grouped below:

WPF Host Application Setup:

  • Added a new WPF application project (SimpleWpfXamlIslandApp) with basic WPF setup files (App.xaml, App.xaml.cs, MainWindow.xaml, MainWindow.xaml.cs, AssemblyInfo.cs, and project file) to serve as the host for WinUI content. [1] [2] [3] [4] [5] [6]
  • Implemented WinUIControlHost, a custom HwndHost that embeds WinUI controls into the WPF window using DesktopWindowXamlSource.
  • Added XamlApp class to initialize WinUI resources and styles for the embedded controls.

WinUI Library and Custom Controls:

  • Added a new WinUI library project (WinUILib) with its own project file.
  • Implemented MyCustomButton, a custom WinUI button, and CustomListView, a WinUI list view that uses a custom data template. [1] [2]
  • Added ListViewStyles.xaml, defining a data template for the custom list view, which includes the custom button.

Solution Structure:

  • Added a new Visual Studio solution file (SimpleWpfXamlIsland.sln) to group the WPF app and WinUI library projects.

Target Release

Please specify which release this PR should align with. e.g., 1.0, 1.1, 1.1 Preview 1.

Checklist

Note that /azp run currently isn't working for this repo.

@rmarinho
Copy link
Member Author

rmarinho commented Aug 5, 2025

Need some help figuring how to get the template on the library to work . seems we need a way to specify the XamlMetadaProvider.

cc @JesseCol

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a side question beyond this PR, but would it be better to start using slnx? https://devblogs.microsoft.com/visualstudio/new-simpler-solution-file-format/

{
_controller = Microsoft.UI.Dispatching.DispatcherQueueController.CreateOnCurrentThread();

var xamlApp = new XamlApp();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to just be newed up and abandoned?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at other samples yes.


void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_winUIControl = new WinUIControlHost(ControlHostElement.ActualHeight, ControlHostElement.ActualWidth);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to notify this of size changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the height and width are actually not used.. resize seem to just work when I resize the window.

{
public CustomListView()
{
ItemTemplate = Application.Current.Resources["ListViewItemTemplate"] as Microsoft.UI.Xaml.DataTemplate;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird, why aren't you just defining CustomListView as a templated control and using a style, then you can just directly specify the item template property as the data template in XAML without this code-behind look-up?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yap sure can try that, this was more copy what MAUI is doing.


namespace SimpleWpfXamlIslandApp
{
internal class XamlApp : Microsoft.UI.Xaml.Application, IXamlMetadataProvider

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a XAML Islands expert, but this seems weird. Should this not just be a file->new winui app but then changed to a not have the output type and manifest? But still just use the regular app.xaml and it's generated partial class over trying to manually do all this? Then the reference to the Resource Dictionary for WinUI and your own ListViewStyles.xaml directly should be possible right over trying to include the xbf?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is more of a try to figure out how this should work. I did some commits to try that, see WinUIApp, but that didn t work either, the partial xaml is never parsed since InitializeComponent is never called. When we initialise
WindowsXamlManager.InitializeForCurrentThread it "jumps" to on launch method of App.xaml.cs.

WE are doing some talks on the WindowsAppSDK office hours chat to figure out this.

<UseWinUI>true</UseWinUI>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4654" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the latest CsWinRT not be here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe? Whats CsWinRT ?

Comment on lines 42 to 62
var grid = new Microsoft.UI.Xaml.Controls.Grid();
grid.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Colors.LightGray);
grid.RowDefinitions.Add(new Microsoft.UI.Xaml.Controls.RowDefinition { Height = new Microsoft.UI.Xaml.GridLength(1, Microsoft.UI.Xaml.GridUnitType.Star) });
grid.RowDefinitions.Add(new Microsoft.UI.Xaml.Controls.RowDefinition { Height = new Microsoft.UI.Xaml.GridLength(1, Microsoft.UI.Xaml.GridUnitType.Star) });

var winUiButton = new MyCustomButton();
winUiButton.Click += (s, e) =>
{
System.Windows.MessageBox.Show("Hello from WinUI!");
};
grid.Children.Add(winUiButton);

var listView = new CustomListView {

ItemsSource = new List<string> { "Item 1", "Item 2", "Item 3" },
};

Microsoft.UI.Xaml.Controls.Grid.SetRow(listView, 1);
grid.Children.Add(listView);

_xamlSource.Content = grid;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be nice to just define this in a XAML usercontrol or page and then just instantiate that here to set as the content then doing this in all code-behind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yap , will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants