0

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 ?

Example project in question here.

13
  • You are not getting any errors and not see anything in the Window. Do you see the Window? Usually issues like this is the Window is not in the front. The variable _source is not used. I think what you are seeing is _source and not window. What is args? The window may not be initialized properly : _source.Initialize(Win32Interop.GetWindowIdFromWindow(args)); Commented Sep 3 at 12:20
  • The window is being created but it's empty, I know it's my window because I can change it's name for instance. I am actually unsure what _source does, I just know it's needed for the XAML island to work, if this variable isn't being initialized my app crashes. I can add other controls and they show up but this DataGrid control doesn't. Commented Sep 3 at 12:44
  • Upon more debugging it seems something is being rendered in my window because I can copy some of the text that's supposed to show up from it. Commented Sep 3 at 13:16
  • You're simply adding a "default" grid to the Window; with a DataGrid. That's a lot of assuming in terms of the actual size, etc. Commented Sep 3 at 15:13
  • I tried changing it's size, it makes no difference. Again the same code displays it just fine when not using XAML islands. Commented Sep 4 at 5:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.