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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Samples/Islands/SimpleWpfXamlIsland/SimpleWpfXamlIsland.sln

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/

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36401.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleWpfXamlIslandApp", "SimpleWpfXamlIslandApp\SimpleWpfXamlIslandApp.csproj", "{4B331F0D-098B-D622-EBB0-61D14FA2C621}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUILib", "WinUILib\WinUILib.csproj", "{6CEC863F-D767-463E-9E3E-D955E045CCF8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4B331F0D-098B-D622-EBB0-61D14FA2C621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B331F0D-098B-D622-EBB0-61D14FA2C621}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B331F0D-098B-D622-EBB0-61D14FA2C621}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B331F0D-098B-D622-EBB0-61D14FA2C621}.Release|Any CPU.Build.0 = Release|Any CPU
{6CEC863F-D767-463E-9E3E-D955E045CCF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CEC863F-D767-463E-9E3E-D955E045CCF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CEC863F-D767-463E-9E3E-D955E045CCF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CEC863F-D767-463E-9E3E-D955E045CCF8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {613D332E-BCD6-44EA-A02D-11A32EE2469E}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="WPF button"
Margin="10"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="Button_Click"/>
<Border Name="ControlHostElement"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="LightGray"
BorderThickness="3"
DockPanel.Dock="Right"/>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.UI.Dispatching;
using SimpleWpfXamlIslandApp;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherQueueController _controller;
WinUIControlHost _winUIControl;

public MainWindow()
{
_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.

InitializeComponent();
Loaded += MainWindow_Loaded;
}

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.

ControlHostElement.Child = _winUIControl;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello from WPF!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.26100.0</TargetFramework>

<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<UseWinUI>true</UseWinUI>
<EnableDefaultXamlItems>false</EnableDefaultXamlItems>
<WindowsPackageType>None</WindowsPackageType>

<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'net8.0'))">
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseRidGraph>true</UseRidGraph>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4654" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WinUILib\WinUILib.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using Windows.UI;
using WinUILib;

namespace SimpleWpfXamlIslandApp
{
public class WinUIControlHost : HwndHost
{
int hostHeight, hostWidth;

Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource _xamlSource;


public WinUIControlHost(double height, double width)
{
hostHeight = (int)height;
hostWidth = (int)width;
_xamlSource = new Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource();
}

protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
var id = new Microsoft.UI.WindowId((ulong)hwndParent.Handle);
_xamlSource.Initialize(id);

InitIslandSampleCode();
//_xamlSource.SiteBridge.ResizePolicy = Microsoft.UI.Content.ContentSizePolicy.ResizeContentToParentWindow;
//_xamlSource.SiteBridge.Show();

return new HandleRef(null, (nint)_xamlSource.SiteBridge.WindowId.Value);
}

void InitIslandSampleCode()
{
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.

}

protected override void DestroyWindowCore(HandleRef hwnd)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Markup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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.

{
public XamlApp()
{
_xamlMetaDataProvider = new Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider();
_windowsXamlManager = WindowsXamlManager.InitializeForCurrentThread();
}

override protected void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
this.Resources.MergedDictionaries.Add(new Microsoft.UI.Xaml.Controls.XamlControlsResources());
this.Resources.MergedDictionaries.Add(new Microsoft.UI.Xaml.ResourceDictionary {
Source = new Uri("ms-appx:///WinUILib/ListViewStyles.xbf")
});
}

IXamlType IXamlMetadataProvider.GetXamlType(string fullName)
{
var xamlType = _xamlMetaDataProvider.GetXamlType(fullName);
return xamlType;
}

IXamlType IXamlMetadataProvider.GetXamlType(System.Type type)
{
var xamlType = _xamlMetaDataProvider.GetXamlType(type);
return xamlType;
}

XmlnsDefinition[] IXamlMetadataProvider.GetXmlnsDefinitions()
{
return _xamlMetaDataProvider.GetXmlnsDefinitions();
}

WindowsXamlManager _windowsXamlManager;
IXamlMetadataProvider _xamlMetaDataProvider;
}
}
18 changes: 18 additions & 0 deletions Samples/Islands/SimpleWpfXamlIsland/WinUILib/CustomListView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinUILib
{
public class CustomListView : ListView
{
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.

}
}
}
15 changes: 15 additions & 0 deletions Samples/Islands/SimpleWpfXamlIsland/WinUILib/ListViewStyles.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:WinUILib">

<DataTemplate x:Key="ListViewItemTemplate">
<Grid>
<local:MyCustomButton Content="{Binding .}"/>
</Grid>
</DataTemplate>


</ResourceDictionary>
19 changes: 19 additions & 0 deletions Samples/Islands/SimpleWpfXamlIsland/WinUILib/MyCustomButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace WinUILib
{
public partial class MyCustomButton : Microsoft.UI.Xaml.Controls.Button
{
public MyCustomButton()
{
Content = "Click Me WinUI";
}
}
}
13 changes: 13 additions & 0 deletions Samples/Islands/SimpleWpfXamlIsland/WinUILib/WinUILib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>WinUILib</RootNamespace>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<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 ?

<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
</ItemGroup>
</Project>