I have the following setup : I am display WinUI3 elements from a C# Class Library in a Win32 C++ app using XAML islands, the control that I want to use is a CommunityToolkit.WinUI.UI.Controls DataGrid but this is only available for C# which is why I need to use a C# Class Library.
I have this function which gets called in my Win32 app :
public static int ShowWindow(nint args, int sizeBytes)
#pragma warning restore IDE0060 // Remove unused parameter
{
// Initialize WinAppSDK 1.6 or 1.7
if (!Bootstrap.TryInitialize(0x00010007, string.Empty, new PackageVersion(), Bootstrap.InitializeOptions.None, out var hr) &&
!Bootstrap.TryInitialize(0x00010006, string.Empty, new PackageVersion(), Bootstrap.InitializeOptions.OnNoMatch_ShowUI, out hr))
return hr;
if (_app == null)
{
_app = new DummyApp(); // Optional: enables WinUI 3 styles
DispatcherQueueController.CreateOnCurrentThread();
}
var _source = new DesktopWindowXamlSource();
_source.Initialize(Win32Interop.GetWindowIdFromWindow(args));
var window = new Microsoft.UI.Xaml.Window();
var sampleData = new List<Person>
{
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 25 },
new Person { Name = "Charlie", Age = 35 }
};
var dataGrid = new CommunityToolkit.WinUI.UI.Controls.DataGrid
{
AutoGenerateColumns = true,
ItemsSource = sampleData,
Margin = new Thickness(20)
};
var grid = new Microsoft.UI.Xaml.Controls.Grid
{
Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.White)
};
grid.Children.Add(dataGrid);
window.Content = grid;
window.Activate();
return 0;
}
It compiles and runs except that the DataGrid doesn't show up, the window I am creating is empty, the same code works and displays the DataGrid correctly if I do this in a C# WinUI3 app. Any idea why ?