问题
In cppwinrt (xaml not yet available) I have a handler for the PointerEntered event of a Button, and it works fine. But in attempting to remove the default hover behavior of this Button, which displays as an image, it seems I may need to handle the OnPointerEntered event instead - I have seen solutions that involve using a xaml trigger and this would seem to be a code equivalent. But OnPointerEntered has a different argument list, and I can't find a way to invoke it that will build. Here is how the PointerEntered event handle is successfully declared:
button.PointerEntered([&](winrt::Windows::Foundation::IInspectable const & sender, Windows::UI::Xaml::RoutedEventArgs const & args) { EnteredButton(); });
That builds and correctly calls EnteredButton. But
button.OnPointerEntered([&](winrt::Windows::UI::Xaml::Input::PointerRoutedEventArgs const & e)
{
EnteredButton();
});
will not build - I am declaring it incorrectly but am not sure how, after trying a number of variants. (Using the fully-qualified names is maybe not necessary, but seems not to hurt in either case). Can OnPointerEntered be used in cppwinrt, and how would it be declared if so? I have tried using IPointerRoutedEventArgs instead of PointerRoutedEventArgs with no effect. Or - if anyone knows a different way to disable the hover effect in a Button created with cppwinrt, that would work.
回答1:
You can't do what you're asking. Echoing IInspectable's comment:
- PointerEntered is an event on Windows::UI::Xaml::UIElement. Your code is correctly declaring a handler for it.
- OnPointerEntered is not an event, so there is no way to handle it. It is an overridable method for which you can provide your own implementation.
来源:https://stackoverflow.com/questions/47931777/how-to-declare-handler-for-onpointerentered-using-cppwinrt