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

Is it possible to programatically create a xaml page?

我可能在下一行写的毫无意义:

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

看代码就明白了: 点号 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>(); });
}

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

安装了CppWinRT VSIX就简单了。然后创建一个 "View model" 并编辑文件。您不应从 winrt::Windows::UI::Xaml::Controls::Page class 派生结构,因为 winrt::xaml_typename<> 需要 <>.

中的 WinRT class