These two code fragments seems to be having same affect
if (!m_mainWindow)
{
m_mainWindow = winrt::make<App1::implementation::MainWindow>();
}
if (!m_mainWindow.Content())
{
m_mainWindow.Content(winrt::make<App1::implementation::ShellPage>());
}
m_mainWindow.Activate();
if (!m_mainWindow)
{
m_mainWindow = App1::MainWindow();
}
if (!m_mainWindow.Content())
{
m_mainWindow.Content(App1::ShellPage());
}
m_mainWindow.Activate();
Is there a reason I should be using winrt::make instead of directly calling constructors. Microsoft docs seems to insists I should be using winrt::make, but why?
Those two code fragments are equivalent only if your projected opted into uniform construction, using cppwinrt's "-opt" flag as described here: https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-apis#opt-in-to-uniform-construction-and-direct-implementation-access
If you haven't opted in,the second code fragment will call RoGetActivationFactory
to retrieve the factory and create an instance of the class, followed by ActivateInstance
and QueryInterface
, which is typically undesirable when you are dealing directly with that component's implementation. That page does a good job of explaining all the details.