如何添加 C++/WinRT 按钮单击处理程序?
How do you add a C++/WinRT Button Click handler?
如何将 Microsoft ButtonBase.Click Event 的 C++/WinRT 信息翻译成原型声明和定义?
在 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;
进入点击处理程序。 Microsoft文档的第一行,即
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,并指示"The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers." 但是,有现有按钮 myButton 没有代码隐藏文件。
How does one translate the Microsoft ButtonBase.Click Event information for C++/WinRT into a prototype declaration and definition?
不确定 prototype declaration and definition
是什么意思,因为给定的信息本身就是原型。
在 this 页面中,您可以找到有关 C++/Winrt 中事件处理的更多信息。 Link 解释了如何使用委托处理和撤销事件,并给出了 C++/WinRT
的按钮单击事件处理示例。总之你需要有处理程序并通过委托注册它。
// 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 });
更新: 当用户尝试访问属性和事件处理程序时,我尝试检查错误 The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers
。我可以通过在安装 cppwinrt 扩展后创建空白应用程序 (C++/Winrt) 轻松重现此问题。
我检查了 photo editor sample app from ms github to further down debug this problem. In this sample app I failed to load XAML design mode. First it requested me to update my Windows version to 1809, but I failed to update. So I decided to rollback to this 不需要 Windows 1809 更新的提交 ID。
此后 XAML 设计因 Some assembly references are missing. Building to restore the NuGet cache might resolve this issue.
错误而失败。研究将我引向了这个 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 in MS docs github, I got following response from them and according 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.
如何将 Microsoft ButtonBase.Click Event 的 C++/WinRT 信息翻译成原型声明和定义?
在 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;
进入点击处理程序。 Microsoft文档的第一行,即
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,并指示"The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers." 但是,有现有按钮 myButton 没有代码隐藏文件。
How does one translate the Microsoft ButtonBase.Click Event information for C++/WinRT into a prototype declaration and definition?
不确定 prototype declaration and definition
是什么意思,因为给定的信息本身就是原型。
在 this 页面中,您可以找到有关 C++/Winrt 中事件处理的更多信息。 Link 解释了如何使用委托处理和撤销事件,并给出了 C++/WinRT
的按钮单击事件处理示例。总之你需要有处理程序并通过委托注册它。
// 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 });
更新: 当用户尝试访问属性和事件处理程序时,我尝试检查错误 The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers
。我可以通过在安装 cppwinrt 扩展后创建空白应用程序 (C++/Winrt) 轻松重现此问题。
我检查了 photo editor sample app from ms github to further down debug this problem. In this sample app I failed to load XAML design mode. First it requested me to update my Windows version to 1809, but I failed to update. So I decided to rollback to this 不需要 Windows 1809 更新的提交 ID。
此后 XAML 设计因 Some assembly references are missing. Building to restore the NuGet cache might resolve this issue.
错误而失败。研究将我引向了这个 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 in MS docs github, I got following response from them and according 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.