c++-winrt - 是否可以以编程方式创建 xaml 页面?

标签 c++-winrt

我可能在接下来的几行中写不出任何意义:

我尝试从 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 模型”并编辑文件。您不应该从 winrt::Windows::UI::Xaml::Controls::Page 派生结构类因为winrt::xaml_typename<><> 中需要一个 WinRT 类.

关于c++-winrt - 是否可以以编程方式创建 xaml 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53966212/

相关文章:

uwp - C++/WinRT 控制台 UWP 应用的 AppxManifest.xml 中的入口点是什么?

c++ - 如何编写异步操作?

C++/WinRT,部分 Windows SDK 17134 与 Visual Studio 15.8 Preview 3 不兼容

azure - 设置从 native 应用程序进行 Azure AD 身份验证的重定向 URI

c# - 如何使用 Windows 运行时通过 C# 实现 C++ API?

c++-cx - 将 C++/CX "delegate"对象转换为 C++/WinRT 中的等效对象

uwp - 无法激活 IDL 中定义的投影类型

c++ - 在 Win32 程序中使用 XAML 托管 API 导航到页面会导致访问冲突

c++ - 数组作为 ValueSet 中的值

c++ - 无法将 std::vector<bool> 传递给 winrt::array_view