Search code examples
uwpc++-winrt

How to access Windows namespace from C++/WinRT component?


I have created a blank C++/WinRT component project via Visual Studio 2019.

I add this line to Class.cpp:

    using namespace Windows::UI::Xaml::Controls;

Giving me this error:

Error   C2653   'Windows': is not a class or namespace name

This is similar to this question, except that question was for C++/CX.

Any ideas?


Solution

  • The entirety of the C++/WinRT projection resides in the winrt namespace. In other words, the Windows Runtime 'namespace' Windows.UI.Xaml.Controls is projected into winrt::Windows::UI::Xaml::Controls.

    To access that namespace, either use

    using namespace winrt::Windows::UI::Xaml::Controls;
    

    or merge the winrt namespace into the global namespace first:

    using namespace winrt;
    using namespace Windows::UI::Xaml::Controls;
    

    Either one will cause you a fair amount of headaches once you move on to incorporate WinUI into your application. Due to types with the same name showing up under both the winrt::Windows::UI::Xaml::Controls as well as winrt::Microsoft::UI::Xaml::Controls namespaces, the poor compiler gets lost in a sea of ambiguity.