uwp - Pointer Released On RichEditBox UWP
问题描述
I would like to use the PointerReleased event in a RichEditBox but it does not work.
Xaml:
<RichEditBox x:Name="RebText" PointerReleased="RebText_PointerReleased"/>
Xaml.cs:
private void RebText_PointerReleased(object sender, PointerRoutedEventArgs e)
{
throw new NotImplementedException();
}
Thanks in advance!
解决方案
Reson it doesn't work:
PointerRoutedEventArgs
has a Handled
bool
Property. If any PointerEventHandler
marks this Handled
as true, the event handler chain doesn't proceed anymore.
Now, the designer of the RichEditBox
control had to hook the PointerReleased
event for some obvious reason, and in the handler, he must have marked the Handled
to true. As a result, the handler you added to the event won't be invoked.
This also happens in other controls. An example is: you can't hook the PointerWheelChanged
event of a ScrollViewer
by just simply adding an EventHandler
to it, like :
myScrollViewer.PointerWheelChanged += Some_Handler;
The handler will be added, but it won't be invoked for the same reason.
Solution:
All UIElement
s have an AddHandler method. This method also adds EventHandler
s to events, just like the +=
operator does, but the benefit is, you can:
have the provided handler be invoked even if the event is handled elsewhere
So, what you should do is, something like:
RebText.AddHandler(RichEditBox.PointerReleasedEvent, new PointerEventHandler(RebText_PointerReleased), true);
and then define RebText_PointerReleased
:
private void RebText_PointerReleased(object sender, PointerRoutedEventArgs e)
{
// your event handling code here
}
The third argument is where you specified that you want this handler to be invoked even if the event is marked handled.
Note:, it's not a good practice to handle an already handled event, because, as the doc says:
Do not routinely ask to rehandle a routed event, because it interferes with the intended design of the Windows Runtime event system for control compositing.
推荐阅读
- python - Python:值转换为列和标志
- javascript - Fetch in JS自动添加当前页面url,如何去掉
- excel - Linux 环境中的 java.lang.reflect.InvocationTargetException 但适用于 Windows
- java - Android - 如何下载图像并将其保存在所需位置?
- android - 最后一次出现在聊天应用程序中的替代实现
- go - GCP:Go 中的存储桶创建
- r - 如何在 Kaggle 中运行 R 脚本文件
- sqlite - 在 Windows 上打包应用程序后的 Electron 和 sqlite3 问题
- python - selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:instagram bot
- c# - 如何获取可用分析器的列表?