首页 > 解决方案 > 是否可以以编程方式创建 xaml 页面?

问题描述

我可能在下一行写没有意义:

我试图从 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>(); });
}

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

标签: c++-winrt

解决方案


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


推荐阅读