我在xamarin.forms中实现了一个自定义的可单击标签类,以及一个自定义呈现器,它添加了一个
RippleDrawable
作为控制
Foreground
. 我正在创建
波纹状
使用以下代码:
public static Drawable CreateRippleDrawable(Context context)
{
var typedValue = new TypedValue();
context.Theme.ResolveAttribute(Resource.Attribute.SelectableItemBackground, typedValue, true);
var rippleDrawable = context.Resources.GetDrawable(typedValue.ResourceId, context.Theme);
return rippleDrawable;
}
在我的自定义渲染器中,我指定了可绘制的
this.Control.Foreground = DrawableHelper.CreateRippleDrawable(this.Context);
并在用户触摸控件时更新Ripple
private void LinkLabelRenderer_Touch(object sender, TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
this.Pressed = true;
}
if (e.Event.Action == MotionEventActions.Cancel)
{
this.Pressed = false;
}
if (e.Event.Action == MotionEventActions.Up)
{
this.Ripple.SetHotspot(e.Event.GetX(), e.Event.GetY());
this.Pressed = false;
// raise the event of the Xamarin.Forms control
}
}
现在,每当我单击控件时,将显示波纹,这是预期的行为,但如果我触摸(轻敲或长按)控件的父级(例如
StackLayout
,
Grid
或包含标签的任何布局,包括其父级
Layout
,
Page
或
View
)将触发涟漪动画。无论如何,事件处理程序
LinkLabelRenderer_Touch
在本例中未调用时,仅当触摸实际控件时。
我可以通过添加一个空的
GestureRecognizer
对于各自的家长,我真的不喜欢这个解决方案,因为这只是一个黑客。更糟的是,这是一种黑客行为,每当我使用控件时,我都必须记住这一点。
我怎样才能防止
波纹状
在触摸父对象时显示?