我正在开发Windows Universal App
(Windows 8.1 + Winwdows Phone 8.1)。我可以在Click
上检测到Button
事件:
<Button x:Name="menuButton" Click="menuButton_Click" />
我也想检测&#34;按&#34;事件和&#34;发布&#34;该按钮上的事件。按下按钮后,我必须在我的应用程序中更改图像。释放按钮后,图像将返回其初始状态。我检查了与Button
类相关的方法,但似乎没有什么可以帮助我。提前谢谢。
答案 0 :(得分:1)
你必须像这样实现它
public class MyButton : Button
{
public event RoutedEventHandler MyPointerPressed;
public event RoutedEventHandler MyPointerReleased;
public event RoutedEventHandler MyPointerExited;
public event RoutedEventHandler MyPointerMoved;
public event EventHandler HoldingStarted;
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
base.OnPointerPressed(e);
MyPointerPressed?.Invoke(this, e);
}
protected override void OnPointerMoved(PointerRoutedEventArgs e)
{
base.OnPointerMoved(e);
MyPointerMoved?.Invoke(this, e);
}
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
base.OnPointerReleased(e);
MyPointerReleased?.Invoke(this, e);
}
protected override void OnPointerExited(PointerRoutedEventArgs e)
{
base.OnPointerExited(e);
MyPointerExited?.Invoke(this, e);
}
protected override void OnHolding(HoldingRoutedEventArgs e)
{
if (e.HoldingState == HoldingState.Started)
{
HoldingStarted?.Invoke(this, new EventArgs());
return;
}
base.OnHolding(e);
}
}