Initialization of UWP C++ XAML application

UWP C++ applications based on “DirectX 11 and XAML App” or “XAML App for OpenGL ES“ project templates have some partial App class defined in user code and in generated file App.g.h:

partial ref class App :  public ::Windows::UI::Xaml::Application,
    public ::Windows::UI::Xaml::Markup::IXamlMetadataProvider
{
public:
    void InitializeComponent();
    [Windows::Foundation::Metadata::DefaultOverload]
    virtual ::Windows::UI::Xaml::Markup::IXamlType^ GetXamlType(::Windows::UI::Xaml::Interop::TypeName type);
    virtual ::Windows::UI::Xaml::Markup::IXamlType^ GetXamlType(::Platform::String^ fullName);
    virtual ::Platform::Array<::Windows::UI::Xaml::Markup::XmlnsDefinition>^ GetXmlnsDefinitions();
private:
    ::XamlTypeInfo::InfoProvider::XamlTypeInfoProvider^ _provider;
    bool _contentLoaded;
};

the user code:

ref class App sealed
{
public:
    App();
    virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;
    ...
};

App object is created in main function defined in generated App.g.hpp file:

int __cdecl main(::Platform::Array<::Platform::String^>^ args)
{
    (void)args; // Unused parameter
    ::Windows::UI::Xaml::Application::Start(ref new ::Windows::UI::Xaml::ApplicationInitializationCallback(
        [](::Windows::UI::Xaml::ApplicationInitializationCallbackParams^ p) {
            (void)p; // Unused parameter
            auto app = ref new ::MyApp::App();
        }));
}

Looks like that this code sets up Windows::UI::Xaml::Window::Current, so App class sets up its content from App::OnLaunched handler:

void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
    ...

    if (mPage == nullptr)
    {
        mPage = ref new MainPage();
    }

    // Place the page in the current window and ensure that it is active.
    Windows::UI::Xaml::Window::Current->Content = mPage;
    Windows::UI::Xaml::Window::Current->Activate();
}

Do not mix this Windows::UI::Xaml::Window::Current with CoreWindow, because CoreWindow does not relate to XAML somehow and does not have any kind of Content property. Looks like that CoreWindow  is a system-level surrounding application window that servers as the application event source and can be used for DirectX drawing, for example, cross platform “OpenGLES 2 Application (Androis, iOS, Windows Universal)” (that finally works via DirectX) does not have XAML and uses another initialization scenario. Its App class implements interface Windows::ApplicationModel::Core::IFrameworkView  and its main function defined in user code does the following:

// Implementation of the IFrameworkViewSource interface, necessary to run our app.
ref class SimpleApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
{
public:
    virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView()
    {
        return ref new App();
    }
};

// The main function creates an IFrameworkViewSource for our app, and runs the app.
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
    auto simpleApplicationSource = ref new SimpleApplicationSource();
    CoreApplication::Run(simpleApplicationSource);
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *