Reputation: 397
How does one translate the Microsoft ButtonBase.Click Event information for C++/WinRT into a prototype declaration and definition?
In Visual Studio Community 2017, version 15.9.5, I created a standard project, viz., Blank App (C++/WinRT), which has a button (myButton) as part of it. I want to learn how to add a second button. I have added the XAML code for a second button (myButton2), but thereafter I am stuck.
<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>
I could just copy the code from the first button that the Blank App (C++/WinRT) provided. But, if I did not have the first button as an example, I would not know how I would translate the Microsoft documentation, viz.,
// 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;
into a Click handler. The first line of the Microsoft documentation, viz.,
event_token Click(RoutedEventHandler const& handler) const;
is, I take it, the format to be used in creating the handler. It does not look like the handler already in the project, viz.,
void ClickHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
I thought that it could be added to the .idl file. But, the .idl file does not have the first button in it.
The example code with Microsoft provides is for C#, even when "C++/WinRT" is selected at the top-right of the page.
The new version of C++/WinRT has a lot of documentation, almost too much. Coming from having used C#/WPF/XAML, I cannot see my way clearly to doing similar work with C++/WinRT.
Finally, the project Properties tab is of no help. In C# the events were listed and choosing one would result in a declaration and basic definition being created in the code. For C++/WinRT, when the cursor is on the control in the XAML page, the events section of the Properties page shows the name of the control, myButton or myButton2, and indicates "The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers." But, there is no code-behind file for the existing button, myButton.
Upvotes: 2
Views: 2933
Reputation: 2345
How does one translate the Microsoft ButtonBase.Click Event information for C++/WinRT into a prototype declaration and definition?
Not sure what do you mean by prototype declaration and definition
since given information itself is prototype.
In this page you can find more information about event handling in C++/Winrt. Link explains about handling and revoking events with delegates and gives example of Button click event handling for C++/WinRT
. In short you need to have handler and register it through 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: I tried to check regarding error The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers
when user tries to access Properties and Event Handlers. I could easily reproduce this issue by creating Blank App (C++/Winrt) after installing cppwinrt extension.
I checked 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 commit id which does not require Windows 1809 update.
After this XAML design failed with Some assembly references are missing. Building to restore the NuGet cache might resolve this issue.
error. Research lead me to this issue which is closed with following comment
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.
After opening issue in MS docs github, I got following response from them and according page of MS docs was updated.
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.
Upvotes: 1