我正在开发一个简单的 C++/WinRT UWP 应用程序,以了解如何为这些事件的实际处理程序链接 XAML 事件和 C++/WinRT 源代码。
我有一个包含蓝色矩形的简单网格的 XAML 源。在蓝色矩形旁边是一个按钮,可从早期工作中单击以在单击该按钮时触发处理程序。那工作得很好。
现在我想处理鼠标指针事件。但是,C++/WinRT 源代码生成会生成链接错误。我假设这意味着源代码中的处理程序没有正确的接口。
然而,到目前为止,我一直无法预测正确的签名,而且我已经没有山羊来获取必要的内脏了。
有人能告诉我指针事件的事件处理程序应该是什么吗?
我想为我目前正在探索的以下指针事件提供必要的接口:
- 指针已退出
- 指针释放
- 指针按下
MainPage.h 文件包含以下声明:
namespace winrt::BlankAppTouch1::implementation
{
struct MainPage : MainPageT<MainPage>
{
MainPage();
int32_t MyProperty();
void MyProperty(int32_t value);
void ClickHandler(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& args);
// Handler for pointer exited event.
void PointerExitedHandler(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& token);
// Handler for pointer released event.
void touchRectangle_PointerReleased(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& e);
// Handler for pointer pressed event.
void touchRectangle_PointerPressed(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& e);
};
}
MainPage.cpp 文件包含以下源:
#include "pch.h"
#include "MainPage.h"
using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Shapes;
namespace winrt::BlankAppTouch1::implementation
{
MainPage::MainPage()
{
InitializeComponent();
}
int32_t MainPage::MyProperty()
{
throw hresult_not_implemented();
}
void MainPage::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}
void MainPage::ClickHandler(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
}
// Handler for pointer exited event.
void MainPage::PointerExitedHandler(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& token)
{
Rectangle rect = winrt::unbox_value<Rectangle>(sender);
// Pointer moved outside Rectangle hit test area.
// Reset the dimensions of the Rectangle.
if (nullptr != rect)
{
rect.Width(200);
rect.Height(100);
}
}
// Handler for pointer released event.
void MainPage::touchRectangle_PointerReleased(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& e)
{
Rectangle rect = winrt::unbox_value<Rectangle>(sender);
// Reset the dimensions of the Rectangle.
if (nullptr != rect)
{
rect.Width(200);
rect.Height(100);
}
}
// Handler for pointer pressed event.
void MainPage::touchRectangle_PointerPressed(Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const& e)
{
Rectangle rect = winrt::unbox_value<Rectangle>(sender);
// Change the dimensions of the Rectangle.
if (nullptr != rect)
{
rect.Width(250);
rect.Height(150);
}
}
}
MainPage.xaml 文件包含以下源:
<Page
x:Class="BlankAppTouch1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BlankAppTouch1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Rectangle x:Name="touchRectangle"
Width="200" Height="200" Fill="Blue"
PointerExited="PointerExitedHandler"
ManipulationMode="All"/>
<Button x:Name="myButton" Click="ClickHandler">Click Me</Button>
</StackPanel>
</Grid>
</Page>
链接错误fatal error LNK1120: 1 unresolved externals
具有以下描述:
未解析的外部符号“公共:__thiscall winrt::Windows::UI::Xaml::Input::PointerEventHandler::PointerEventHandler(struct winrt::BlankAppTouch1::implementation::MainPage ,void (__thiscall winrt::BlankAppTouch1::implementation: :主页::)(struct winrt::Windows::Foundation::IInspectable const &,struct winrt::Windows::UI::Xaml::RoutedEventArgs const &))" (??$?0UMainPage@implementation@BlankAppTouch1@winrt@@P80123 @AEXABUIInspectable@Foundation@Windows@3@ABURoutedEventArgs@Xaml@UI@63@@Z@PointerEventHandler@Input@Xaml@UI@Windows@winrt@@QAE@PAUMainPage@implementation@BlankAppTouch1@5@P86785@AEXABUIInspectable@Foundation@45 @ABURoutedEventArgs@2345@@Z@Z) 在函数“public: void __thiscall winrt::BlankAppTouch1::implementation::MainPageT::Connect(int,struct winrt::Windows::Foundation::IInspectable const &)”中引用( ?Connect@?$MainPageT@UMainPage@implementation@BlankAppTouch1@winrt@@$$V@implementation@BlankAppTouch1@winrt@@QAEXHABUIInspectable@Foundation@Windows@4@@Z)
如果我PointerExited="PointerExitedHandler"
从 XAML 中删除描述Rectangle
.
附录 A:更改和错误
从 XAML 中删除该子句后,我尝试将指针退出处理程序的处理程序放入矩形对象本身。重新编译,我得到了似乎是相同的未解决的外部符号链接错误。
MainPage::MainPage()
{
InitializeComponent();
touchRectangle().PointerExited({ this, &MainPage::PointerExitedHandler });
}
如果我PointerExited()
通过将字母添加x
到处理程序函数的名称(如 中touchRectangle().PointerExited({ this, &MainPage::PointerExitedHandlerx });
)来修改方法中指定的处理程序的名称,我会看到编译器错误,表明标识符PointerExitedHandlerx
不存在,正如预期的那样。
Severity Code Description Project File Line Suppression State
Error C2039 'PointerExitedHandlerx': is not a member of 'winrt::BlankAppTouch1::implementation::MainPage' BlankAppTouch1 d:\users\rickc\documents\vs2017repos\blankapptouch1\blankapptouch1\mainpage.cpp 15
Error C2065 'PointerExitedHandlerx': undeclared identifier BlankAppTouch1 d:\users\rickc\documents\vs2017repos\blankapptouch1\blankapptouch1\mainpage.cpp 15
附录 B:PointerEventHandler 结构
PointerEventHandler
from的定义Windows.UI.Input.2.h
是:
struct PointerEventHandler : Windows::Foundation::IUnknown
{
PointerEventHandler(std::nullptr_t = nullptr) noexcept {}
template <typename L> PointerEventHandler(L lambda);
template <typename F> PointerEventHandler(F* function);
template <typename O, typename M> PointerEventHandler(O* object, M method);
void operator()(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e) const;
};
并PointerRoutedEventArgs
定义为:
#define WINRT_EBO __declspec(empty_bases)
// other source for definitions, etc.
struct WINRT_EBO PointerRoutedEventArgs :
Windows::UI::Xaml::Input::IPointerRoutedEventArgs,
impl::base<PointerRoutedEventArgs, Windows::UI::Xaml::RoutedEventArgs>,
impl::require<PointerRoutedEventArgs, Windows::UI::Xaml::IRoutedEventArgs, Windows::UI::Xaml::Input::IPointerRoutedEventArgs2>
{
PointerRoutedEventArgs(std::nullptr_t) noexcept {}
};
文档链接