winrt::hresult_class_not_registered When I try to use a C++/Winrt User Control In a C++ Win32 application

172 Views Asked by At

I'm using visual studio 2022.

I'm trying to host a WinUI3 Control inside a C-style Win32 Desktop application.

This WinUI3 Control Library has several simple components. These simple components are working fine in the Win32 application. The WinUI3 user control on the other hand fails when the type is declared with a winrt::hresult_class_not_registered exception

void BuildNavigationViewSample(DesktopWindowXamlSource desktopSource)
{
    Grid                        grid;
    RowDefinition               rowDefinition = CreateRow(1, GridUnitType::Star);
    NavigationView              navigationView;
    AutoSuggestBox              autoSuggestBox;
    Frame                       contentFrame;
    Company                     company;    <= works fine
    Thermometer                 thermometer; <= works fine
    PersonViewModel             personViewModel; <= works fine
**  WinUI3UserControl           winUi3UserControl; <= hresult_class_not_registered**

    autoSuggestBox.QueryIcon(CreateSymbolIcon(Symbol::Find));
   
    navigationView.RequestedTheme(ElementTheme::Default);
    navigationView.IsBackButtonVisible(NavigationViewBackButtonVisible::Auto);
    navigationView.PaneDisplayMode(NavigationViewPaneDisplayMode::Left);
    navigationView.PaneTitle(L"Dentrix Navigation  View");
    navigationView.AutoSuggestBox(autoSuggestBox);

    navigationView.MenuItems().Append(CreateNavigationViewItemHeader(L"Patients"));
    navigationView.MenuItems().Append(CreateNavigationItem(L"Home", Symbol::Home));
    navigationView.MenuItems().Append(CreateNavigationItem(L"Manage", Symbol::Manage));

    navigationView.MenuItems().Append(CreateNavigationViewItemHeader(L"Email"));
    navigationView.MenuItems().Append(CreateNavigationItem(L"Mail", Symbol::Mail));
    navigationView.MenuItems().Append(CreateNavigationItem(L"Shopping", Symbol::Shop));

    contentFrame.Content(BuildCommandBarSample());

    navigationView.Content(contentFrame);
    
    grid.RowDefinitions().Append(rowDefinition);

    grid.Children().Append(navigationView);

    grid.SetRow(navigationView, 0);

    grid.UpdateLayout();

    desktopSource.Content(grid);
}

I created a C++/Winrt component library that has the WinUI3 control.

The Win32 project has a reference to the "winmd" meta data file that is the main artifact of compiling the C++ Winrt component library.

When I run the Win32 application it fails with a winrt::hresult_class_not_registered exception.

I expected it not to crash. Therefore I need to know what I need to do to fix this exception.

0

There are 0 best solutions below