c++ - 如何添加 C++/WinRT 按钮单击处理程序?

标签 c++ c++-winrt

如何翻译 Microsoft ButtonBase.Click Event将 C++/WinRT 的信息转换为原型(prototype)声明和定义?

在 Visual Studio Community 2017 版本 15.9.5 中,我创建了一个标准项目,即 Blank App (C++/WinRT),其中有一个按钮 (myButton)。我想学习如何添加第二个按钮。我已经为第二个按钮 (myButton2) 添加了 XAML 代码,但之后我就卡住了。

<Page
    x:Class="X003.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:X003"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel x:Name="stackPan" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"  Background="LightBlue">
        <Button x:Name="myButton" Click="ClickHandler">Click Me</Button>
        <Button x:Name="myButton2" Click="ClickHandler2">Click Me2</Button>
    </StackPanel>
</Page>

我可以从空白应用 (C++/WinRT) 提供的第一个按钮复制代码。但是,如果我没有第一个按钮作为示例,我将不知道如何翻译 Microsoft 文档,即,

// Register
event_token Click(RoutedEventHandler const& handler) const;

// Revoke with event_token
void Click(event_token const& cookie) const;

// Revoke with event_revoker
Click_revoker Click(auto_revoke_t, RoutedEventHandler const& handler) const;

进入点击处理程序。微软文档的第一行,即,

event_token Click(RoutedEventHandler const& handler) const;

是,我认为,用于创建处理程序的格式。它看起来不像项目中已有的处理程序,即,

void ClickHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);

我认为它可以添加到 .idl 文件中。但是,.idl 文件中没有第一个按钮。

Microsoft 提供的示例代码适用于 C#,即使在页面右上角选择了“C++/WinRT”也是如此。

C++/WinRT 的新版本有很多文档,几乎太多了。由于使用过 C#/WPF/XAML,我无法清楚地看到使用 C++/WinRT 进行类似工作的方法。

最后,项目属性选项卡没有帮助。在 C# 中,事件被列出,选择一个将导致在代码中创建声明和基本定义。对于 C++/WinRT,当光标位于 XAML 页面中的控件上时,属性页面的事件部分会显示控件的名称、myButton 或 myButton2,并指示“文档项没有代码隐藏文件。添加一个添加事件处理程序之前的代码隐藏文件和类定义。”但是,现有按钮 myButton 没有代码隐藏文件。

最佳答案

How does one translate the Microsoft ButtonBase.Click Event information for C++/WinRT into a prototype declaration and definition?

不确定 prototype declaration and definition 是什么意思,因为给定的信息本身就是原型(prototype)。

this页面,您可以找到有关 C++/Winrt 中事件处理的更多信息。链接解释了如何使用委托(delegate)处理和撤销事件,并给出了 C++/WinRT 的按钮单击事件处理示例。简而言之,您需要拥有处理程序并通过委托(delegate)进行注册。

// function that handles event
void MainPage::ClickHandler(IInspectable const& /* sender */, RoutedEventArgs const& /* args */)
{
    Button().Content(box_value(L"Clicked"));
}
...
// register above function for event handler
Button().Click({ this, &MainPage::ClickHandler });

UPD:我尝试检查错误文档项没有代码隐藏文件。当用户尝试访问属性和事件处理程序时,在添加事件处理程序之前添加代码隐藏文件和类定义。我可以通过在安装 cppwinrt 后创建空白应用程序 (C++/Winrt) 轻松重现此问题。扩展名。

我检查了photo editor sample app从 ms github 进一步向下调试这个问题。在此示例应用程序中,我无法加载 XAML 设计模式。首先它要求我将我的 Windows 版本更新到 1809,但我没有更新。所以我决定回滚到this不需要 Windows 1809 更新的提交 ID。

在此 XAML 设计失败后,缺少一些程序集引用。构建以恢复 NuGet 缓存可能会解决此问题。 错误。研究让我想到这个issue以下评论结束

This is currently unsupported. The XAML team is currently working on designer support, so you should see this light up in a future update to the Windows SDK.

打开后issue在 MS docs github 中,我得到了他们的以下回复并根据 page更新了 MS 文档。

I've added the following info to the "Blank App (C++/WinRT)" subsection of this topic, since that's the project template that uses XAML as its UI.

"Visual Studio's XAML design surface support for C++/WinRT is close to parity with C#. One exception is the Events tab of the Properties window. With a C# project, you can use that tab to add event handlers; with a C++/WinRT project, that facility is not present. But see Handle events by using delegates in C++/WinRT for info on how to add event handlers to your code."

This change should be live soon. Please let me know if there's anything further I can add.

关于c++ - 如何添加 C++/WinRT 按钮单击处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226053/

相关文章:

c++ - 如何在没有 list 的情况下启用视觉样式

c++ - UML:具有参数依赖关系的模板类之间的继承

C++ for 循环 : 'i' was not declared in this scope

c++ - 视觉 C++ : No devirtualization in obvious cases?

c++ - 使用c++/WinRT Direct3D UWP Game DR模板Direct X11的错误消息

c++ - 如何为 UWP C++/WinRT 应用程序创建 "suitable await_ready function"?

c++ - 当 C++ winrt 页面类与 XAML 页面一起使用时,不是成员错误

uwp - 如何在代码中创建 XAML 自定义控件?

c++ - MongoDB C++,插入时如何添加ISODate值

UWP C++/WinRT - 如何检测是否按下鼠标右键