是否可以以编程方式创建xaml页面?

时间:2018-12-29 02:31:44

标签: c++-winrt

我可能在接下来的几行中写得毫无意义:

我试图从winrt :: Windows :: UI :: Xaml :: Controls :: Page派生一个名为mainpage的结构,并将其传递给winrt :: xaml_typename <>(),在“ <>”是主页。 / p>

当您查看代码时,这是可以理解的: 点号1“(1)”是mainpage.h文件,非常短。 点号2“(2)”是App.cpp文件,只有必需的文件。

//(1) mainpage.h

#pragma once

#include "pch.h"

struct mainpage : winrt::Windows::UI::Xaml::Controls::Page
{
    mainpage() {

        winrt::Windows::UI::Xaml::Controls::Button thebutton = winrt::Windows::UI::Xaml::Controls::Button();
        thebutton.Click([&](const IInspectable& sender, const winrt::Windows::UI::Xaml::RoutedEventArgs& event_arg)
        {
            thebutton.Content(winrt::box_value(L"Clicked"));
        });
    }
};

//(2) App.cpp

#include "pch.h"
#include "mainpage.h"

struct App : winrt::Windows::UI::Xaml::ApplicationT<App>
{
    void OnLaunched(const winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs& event_arg)
    {
        winrt::Windows::UI::Xaml::Controls::Frame root_frame{ nullptr };
        winrt::Windows::UI::Xaml::UIElement content = winrt::Windows::UI::Xaml::Window::Current().Content();
        if (content)
        {
            root_frame = content.try_as<winrt::Windows::UI::Xaml::Controls::Frame>();
        }

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (root_frame == nullptr)
        {
            // Create a Frame to act as the navigation context and associate it with
            // a SuspensionManager key
            root_frame = winrt::Windows::UI::Xaml::Controls::Frame();

            root_frame.NavigationFailed({ this, &App::OnNavigationFailed });

            if (event_arg.PreviousExecutionState() == winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState::Terminated)
            {
                // Restore the saved session state only when appropriate, scheduling the
                // final launch steps after the restore is complete
            }

            if (event_arg.PrelaunchActivated() == false)
            {
                if (root_frame.Content() == nullptr)
                {
                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
                }
                // Place the frame in the current Window
                winrt::Windows::UI::Xaml::Window::Current().Content(root_frame);
                // Ensure the current window is active
                winrt::Windows::UI::Xaml::Window::Current().Activate();
            }
        }
        else
        {
            if (event_arg.PrelaunchActivated() == false)
            {
                if (root_frame.Content() == nullptr)
                {
                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    root_frame.Navigate(winrt::xaml_typename<mainpage>(), box_value(event_arg.Arguments()));
                }
                // Ensure the current window is active
                winrt::Windows::UI::Xaml::Window::Current().Activate();
            }
        }
    }
    void App::OnNavigationFailed(const IInspectable&, const winrt::Windows::UI::Xaml::Navigation::NavigationFailedEventArgs& event_arg)
    {
        throw winrt::hresult_error(E_FAIL, winrt::hstring(L"Failed to load Page ") + event_arg.SourcePageType().Name);
    }
};

int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
    winrt::Windows::UI::Xaml::Application::Start([](auto &&) { winrt::make<App>(); });
}

如果不可能,如何正确创建页面?

1 个答案:

答案 0 :(得分:0)

如果安装了CppWinRT VSIX,这很简单。然后创建一个“视图模型”并编辑文件。您不应该从winrt::Windows::UI::Xaml::Controls::Page类派生结构,因为winrt::xaml_typename<><>中需要WinRT类。

相关问题