I have created a static library(c++/WinRT) and an Universal Windows App [Blank App] (c++/WinRT)project in VS 2019.
Then From Project Universal Window app I added project reference of the static library.
I am getting below error while building the universal app
D:\Dv\Main\Vib\CM\UI\UserInterfaceCppWinRT\Generated Files\XamlTypeInfo.g.cpp(178,52): error C2039: 'UIViewModelCppWinRT': is not a member of 'winrt'
Individually the static library build successfully. Not getting any clue about c++/winrt static library project consumption. Any suggestion?
When you add reference of the static library to main project and build main project successfully, you can find it has generated the StaticLibrary.h file under the \MyProject\MyProject\Generated Files\winrt folder along with other Windows namespaces. So if you want to import the idl(e.g. Class.idl), you don't need to import it manually, since your toolchain is set up to automatically reference any types that are in system namespaces. You only need to import any types that you've defined in your project. For example:
MainPage.idl
namespace WinrtApp
{
[default_interface]
runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
{
MainPage();
Int32 MyProperty;
StaticLibrary1.Class MyClass;
}
}
When you want to access the property from static library, first include the header file and initialize it.
MainPage.h
private:
StaticLibrary1::Class myclass{nullptr};
.cpp:
#include "winrt/StaticLibrary1.h"
myclass = StaticLibrary1::Class();
int property = myclass.MyProperty();
Here is a simple sample that I created, you can download it to check the complete steps.