microsoft / cppwinrt

C++/WinRT
MIT License
1.66k stars 239 forks source link

Can't add a UserControl from a C# Windows Runtime Component #943

Closed michael-hawker closed 3 years ago

michael-hawker commented 3 years ago

Was following this guide: https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/use-csharp-component-from-cpp-winrt (still running in Debug though)

But instead of trying to create a regular class, was trying to create a UserControl component to use in XAML. That would produce these build errors:

1>Generated Files\XamlTypeInfo.g.cpp(270,74): error C2039: 'CSharpIslandRuntimeComponent_XamlTypeInfo': is not a member of 'winrt::CSharpIslandRuntimeComponent'
1>Generated Files\XamlTypeInfo.g.cpp(270,74): error C3083: 'CSharpIslandRuntimeComponent_XamlTypeInfo': the symbol to the left of a '::' must be a type
1>Generated Files\XamlTypeInfo.g.cpp(270,117): error C2039: 'XamlMetaDataProvider': is not a member of 'winrt::CSharpIslandRuntimeComponent'
1>Generated Files\XamlTypeInfo.g.cpp(270,137): error C2259: 'winrt::CppWinRTToolkitIsland::implementation::XamlMetaDataProvider': cannot instantiate abstract class
1>Generated Files\XamlTypeInfo.g.cpp(271,74): error C2039: 'CSharpIslandRuntimeComponent_XamlTypeInfo': is not a member of 'winrt::CSharpIslandRuntimeComponent'
1>Generated Files\XamlTypeInfo.g.cpp(271,74): error C3083: 'CSharpIslandRuntimeComponent_XamlTypeInfo': the symbol to the left of a '::' must be a type
1>Generated Files\XamlTypeInfo.g.cpp(271,117): error C2039: 'XamlMetaDataProvider': is not a member of 'winrt::CSharpIslandRuntimeComponent'
1>Generated Files\XamlTypeInfo.g.cpp(271,137): error C2259: 'winrt::CppWinRTToolkitIsland::implementation::XamlMetaDataProvider': cannot instantiate abstract class

The C++/WinRT project wouldn't compile unless I additionally added the Xaml Type Info header to the MainPage.h file as well (as mentioned in this Stack Overflow):

#include "winrt/CSharpIslandRuntimeComponent.h"
#include "winrt/CSharpIslandRuntimeComponent.CSharpIslandRuntimeComponent_XamlTypeInfo.h"

But then this caused the app to crash at runtime:

> 0x80131040 : The located assembly's manifest definition does not match the assembly reference.

Exception thrown at 0x00007FFF9F2B4B89 in CppWinRTToolkitIsland.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x00000052D38FAB78.
    KernelBase.dll!RaiseException()    Unknown
    vcruntime140d_app.dll!00007fff4e9bb1f0()    Unknown
>   CppWinRTToolkitIsland.exe!winrt::throw_hresult(const winrt::hresult result) Line 4908   C++
    CppWinRTToolkitIsland.exe!winrt::check_hresult(const winrt::hresult result) Line 4979   C++
    CppWinRTToolkitIsland.exe!winrt::Windows::Foundation::IActivationFactory::ActivateInstance<winrt::CSharpIslandRuntimeComponent::ToolkitIsland>() Line 6414  C++
    CppWinRTToolkitIsland.exe!winrt::CSharpIslandRuntimeComponent::ToolkitIsland::<lambda>(const winrt::Windows::Foundation::IActivationFactory & f) Line 37    C++
    CppWinRTToolkitIsland.exe!winrt::CSharpIslandRuntimeComponent::ToolkitIsland <lambda>(const winrt::Windows::Foundation::IActivationFactory &)::<lambda_invoker_cdecl>(const winrt::Windows::Foundation::IActivationFactory & f) Line 37 C++
    CppWinRTToolkitIsland.exe!winrt::impl::factory_cache_entry<winrt::CSharpIslandRuntimeComponent::ToolkitIsland,winrt::Windows::Foundation::IActivationFactory>::call<winrt::CSharpIslandRuntimeComponent::ToolkitIsland (__cdecl*)(winrt::Windows::Foundation::IActivationFactory const &)>(winrt::CSharpIslandRuntimeComponent::ToolkitIsland(*)(const winrt::Windows::Foundation::IActivationFactory &) && callback) Line 6246 C++
    CppWinRTToolkitIsland.exe!winrt::impl::call_factory_cast<winrt::CSharpIslandRuntimeComponent::ToolkitIsland (__cdecl*)(winrt::Windows::Foundation::IActivationFactory const &),winrt::CSharpIslandRuntimeComponent::ToolkitIsland,winrt::Windows::Foundation::IActivationFactory,winrt::CSharpIslandRuntimeComponent::ToolkitIsland <lambda>(const winrt::Windows::Foundation::IActivationFactory &)>(winrt::CSharpIslandRuntimeComponent::ToolkitIsland::winrt::CSharpIslandRuntimeComponent::ToolkitIsland <lambda>(const winrt::Windows::Foundation::IActivationFactory &) && callback) Line 6285    C++
    CppWinRTToolkitIsland.exe!winrt::CSharpIslandRuntimeComponent::ToolkitIsland::ToolkitIsland() Line 39   C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::ActivateType<winrt::CSharpIslandRuntimeComponent::ToolkitIsland>() Line 29  C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::XamlUserType::ActivateInstance() Line 437   C++
    CppWinRTToolkitIsland.exe!winrt::impl::produce<winrt::CppWinRTToolkitIsland::implementation::XamlUserType,winrt::Windows::UI::Xaml::Markup::IXamlType>::ActivateInstance(void * * result) Line 863  C++
    Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo * pType, IInspectable * pOuter, IInspectable * * ppInstance) Line 42 C++
    [Inline Frame] Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo *) Line 29    C++
    Windows.UI.Xaml.dll!DirectUI::XamlParserCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken tokType, XamlQualifiedObject * pqoInstance) Line 49 C++
    [Inline Frame] Windows.UI.Xaml.dll!CFxCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken) Line 1258    C++
    Windows.UI.Xaml.dll!XamlManagedRuntime::CreateInstance(const XamlTypeToken & inXamlType, std::shared_ptr<XamlQualifiedObject> & returnQo) Line 40   C++
    [Inline Frame] Windows.UI.Xaml.dll!XamlType::CreateInstance(std::shared_ptr<XamlQualifiedObject> &) Line 40 C++
    Windows.UI.Xaml.dll!ObjectWriterRuntime::CreateTypeImpl(const XamlLineInfo & lineInfo, const std::shared_ptr<XamlType> & spType, const std::shared_ptr<ObjectWriterCallbacksDelegate> & spCallback, const std::shared_ptr<XamlQualifiedObject> & spRootObjectInstance, std::shared_ptr<XamlQualifiedObject> & spInstance) Line 76   C++
    [Inline Frame] Windows.UI.Xaml.dll!ObjectWriterCommonRuntime::CreateType(const XamlLineInfo &) Line 116 C++
    Windows.UI.Xaml.dll!BinaryFormatObjectWriter::CreateInstanceFromType(const ObjectWriterNode & node) Line 436    C++
    Windows.UI.Xaml.dll!BinaryFormatObjectWriter::WriteNode(const ObjectWriterNode & inNode) Line 136   C++
    Windows.UI.Xaml.dll!CParser::LoadXamlCore(CCoreServices * pCore, const CParserSettings & parserSettings, const Parser::XamlBuffer & buffer, CDependencyObject * * ppDependencyObject, const xstring_ptr_view & strSourceAssemblyName, const std::array<unsigned char,64> &) Line 284    C++
    [Inline Frame] Windows.UI.Xaml.dll!CParser::LoadXaml(CCoreServices *) Line 46   C++
    Windows.UI.Xaml.dll!CCoreServices::ParseXamlWithExistingFrameworkRoot(const Parser::XamlBuffer & buffer, CDependencyObject * pExistingFrameworkRoot, const xstring_ptr_view & strSourceAssemblyName, const xstring_ptr_view & strXamlResourceUri, const bool expandTemplatesDuringParse, CDependencyObject * * ppDependencyObject) Line 3687    C++
    Windows.UI.Xaml.dll!CApplication::LoadComponent(CCoreServices * pCore, CDependencyObject * pComponent, IPALUri * pUri) Line 512 C++
    Windows.UI.Xaml.dll!Application_LoadComponent(CCoreServices * pCore, CDependencyObject * pComponent, const xstring_ptr & strUri, ComponentResourceLocation::Enum resourceLocation) Line 385 C++
    Windows.UI.Xaml.dll!DirectUI::FrameworkApplication::LoadComponent(IInspectable * pComponent, const xstring_ptr & strUri, Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation resourceLocation) Line 553  C++
    Windows.UI.Xaml.dll!DirectUI::FrameworkApplicationFactory::LoadComponentWithResourceLocationImpl(IInspectable * pComponent, Windows::Foundation::IUriRuntimeClass * pUri, Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation resourceLocation) Line 500 C++
    Windows.UI.Xaml.dll!DirectUI::FrameworkApplicationFactory::LoadComponentWithResourceLocation(IInspectable * pComponent, Windows::Foundation::IUriRuntimeClass * pResourceLocator, Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation componentResourceLocation) Line 778    C++
    CppWinRTToolkitIsland.exe!winrt::impl::consume_Windows_UI_Xaml_IApplicationStatics<winrt::Windows::UI::Xaml::IApplicationStatics>::LoadComponent(const winrt::Windows::Foundation::IInspectable & component, const winrt::Windows::Foundation::Uri & resourceLocator, const winrt::Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation & componentResourceLocation) Line 265 C++
    CppWinRTToolkitIsland.exe!winrt::Windows::UI::Xaml::Application::LoadComponent::__l2::<lambda>(const winrt::Windows::UI::Xaml::IApplicationStatics & f) Line 12248  C++
    CppWinRTToolkitIsland.exe!winrt::impl::call_factory<winrt::Windows::UI::Xaml::Application,winrt::Windows::UI::Xaml::IApplicationStatics,void <lambda>(const winrt::Windows::UI::Xaml::IApplicationStatics &)>(winrt::Windows::UI::Xaml::Application::LoadComponent::__l2::void <lambda>(const winrt::Windows::UI::Xaml::IApplicationStatics &) && callback) Line 6264   C++
    CppWinRTToolkitIsland.exe!winrt::Windows::UI::Xaml::Application::LoadComponent(const winrt::Windows::Foundation::IInspectable & component, const winrt::Windows::Foundation::Uri & resourceLocator, const winrt::Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation & componentResourceLocation) Line 12249 C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::MainPageT<winrt::CppWinRTToolkitIsland::implementation::MainPage>::InitializeComponent() Line 31    C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::MainPage::MainPage() Line 12    C++
    CppWinRTToolkitIsland.exe!winrt::impl::heap_implements<winrt::CppWinRTToolkitIsland::implementation::MainPage>::heap_implements<winrt::CppWinRTToolkitIsland::implementation::MainPage>()   C++
    CppWinRTToolkitIsland.exe!winrt::make<winrt::CppWinRTToolkitIsland::implementation::MainPage>() Line 7725   C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::ActivateLocalType<winrt::CppWinRTToolkitIsland::implementation::MainPage>() Line 35 C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::XamlUserType::ActivateInstance() Line 437   C++
    CppWinRTToolkitIsland.exe!winrt::impl::produce<winrt::CppWinRTToolkitIsland::implementation::XamlUserType,winrt::Windows::UI::Xaml::Markup::IXamlType>::ActivateInstance(void * * result) Line 863  C++
    Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo * pType, IInspectable * pOuter, IInspectable * * ppInstance) Line 42 C++
    [Inline Frame] Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo *) Line 29    C++
    Windows.UI.Xaml.dll!DirectUI::NavigationCache::LoadContent(HSTRING__ * descriptor, IInspectable * * ppInstance) Line 148    C++
    Windows.UI.Xaml.dll!DirectUI::NavigationCache::GetContent(DirectUI::PageStackEntry * pPageStackEntry, IInspectable * * ppIInspectable) Line 99  C++
    Windows.UI.Xaml.dll!DirectUI::Frame::PerformNavigation() Line 512   C++
    Windows.UI.Xaml.dll!DirectUI::Frame::StartNavigation() Line 434 C++
    Windows.UI.Xaml.dll!DirectUI::Frame::NavigateImpl(Windows::UI::Xaml::Interop::TypeName sourcePageType, IInspectable * pIInspectable, Windows::UI::Xaml::Media::Animation::INavigationTransitionInfo * navigationTransitionInfo, unsigned char * pCanNavigate) Line 382  C++
    Windows.UI.Xaml.dll!DirectUI::FrameGenerated::Navigate(Windows::UI::Xaml::Interop::TypeName sourcePageType, IInspectable * pParameter, Windows::UI::Xaml::Media::Animation::INavigationTransitionInfo * pInfoOverride, unsigned char * pReturnValue) Line 494   C++
    [Inline Frame] Windows.UI.Xaml.dll!DirectUI::Frame::NavigateImpl(Windows::UI::Xaml::Interop::TypeName) Line 340 C++
    Windows.UI.Xaml.dll!DirectUI::FrameGenerated::Navigate(Windows::UI::Xaml::Interop::TypeName sourcePageType, IInspectable * pParameter, unsigned char * pReturnValue) Line 476   C++
    CppWinRTToolkitIsland.exe!winrt::impl::consume_Windows_UI_Xaml_Controls_IFrame<winrt::Windows::UI::Xaml::Controls::IFrame>::Navigate(const winrt::Windows::UI::Xaml::Interop::TypeName & sourcePageType, const winrt::Windows::Foundation::IInspectable & parameter) Line 6167  C++
    CppWinRTToolkitIsland.exe!winrt::CppWinRTToolkitIsland::implementation::App::OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs & e) Line 74   C++
    CppWinRTToolkitIsland.exe!winrt::impl::produce<winrt::CppWinRTToolkitIsland::implementation::App,winrt::Windows::UI::Xaml::IApplicationOverrides>::OnLaunched(void * args) Line 5047    C++
    Windows.UI.Xaml.dll!DirectUI::FrameworkApplicationGenerated::OnLaunchedProtected(Windows::ApplicationModel::Activation::ILaunchActivatedEventArgs * pArgs) Line 502 C++
    Windows.UI.Xaml.dll!DirectUI::FrameworkView::OnActivated(Windows::ApplicationModel::Core::ICoreApplicationView * pSender, Windows::ApplicationModel::Activation::IActivatedEventArgs * pArgs) Line 267  C++
    [Inline Frame] Windows.UI.Xaml.dll!Microsoft::WRL::Callback::__l2::<lambda_843e2d2bd208f22e7025354e109ff701>::operator()(Windows::UI::Core::ICoreWindow * &&) Line 335  C++
    Windows.UI.Xaml.dll!Microsoft::WRL::Details::DelegateArgTraits<long (__cdecl Windows::Foundation::ITypedEventHandler_impl<Windows::Foundation::Internal::AggregateType<Windows::UI::Core::CoreWindow *,Windows::UI::Core::ICoreWindow *>,Windows::Foundation::Internal::AggregateType<Windows::UI::Core::WindowSizeChangedEventArgs *,Windows::UI::Core::IWindowSizeChangedEventArgs *>>::*)(Windows::UI::Core::ICoreWindow *,Windows::UI::Core::IWindowSizeChangedEventArgs *)>::DelegateInvokeHelper<Windows::Foundation::ITypedEventHandler<Windows::UI::Core::CoreWindow *,Windows::UI::Core::WindowSizeChangedEventArgs *>,<lambda_843e2d2bd208f22e7025354e109ff701>,-1,Windows::UI::Core::ICoreWindow *,Windows::UI::Core::IWindowSizeChangedEventArgs *>::Invoke(Windows::UI::Core::ICoreWindow * <args_0>, Windows::UI::Core::IWindowSizeChangedEventArgs * <args_1>) Line 245  C++
    twinapi.appcore.dll!00007fff99220126()  Unknown
    twinapi.appcore.dll!00007fff9920604d()  Unknown
    rpcrt4.dll!00007fffa072a0e3()   Unknown
    rpcrt4.dll!00007fffa07921cb()   Unknown
    rpcrt4.dll!00007fffa06cb369()   Unknown
    combase.dll!00007fffa0c59d3c()  Unknown
    rpcrt4.dll!00007fffa070b71b()   Unknown
    combase.dll!00007fffa0c21173()  Unknown
    combase.dll!00007fffa0c20efe()  Unknown
    combase.dll!00007fffa0c5e236()  Unknown
    combase.dll!00007fffa0bfa283()  Unknown
    combase.dll!00007fffa0c60f78()  Unknown
    combase.dll!00007fffa0bc6aa6()  Unknown
    combase.dll!00007fffa0c0bd54()  Unknown
    combase.dll!00007fffa0c0bab2()  Unknown
    combase.dll!00007fffa0c0b64d()  Unknown
    combase.dll!00007fffa0c108d7()  Unknown
    Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessMessage(bool bDrainQueue, bool * pbWindowMessagesProcessed, bool * pbInvokeItemProcessed) Line 332    C++
    Windows.UI.dll!Windows::UI::Core::CDispatcher::WaitAndProcessMessagesInternal(bool bRunAlwaysOnce, void * hEventWait) Line 1950 C++
    Windows.UI.dll!Windows::UI::Core::CDispatcher::WaitAndProcessMessages(void * hEventWait) Line 458   C++
    twinapi.appcore.dll!00007fff991fe54a()  Unknown
    SHCore.dll!00007fffa133e689()   Unknown
    kernel32.dll!00007fffa05d7034() Unknown
    ntdll.dll!00007fffa15a2651()    Unknown

Going to do a quick test running in Release mode next...

kennykerr commented 3 years ago

I believe this is a long-standing Xaml compiler bug. Please open an issue here: https://github.com/microsoft/microsoft-ui-xaml

Unfortunately, I'm unable to transfer this one.