Xamarin.forms 与父控件的交互会触发Xamarin.Forns自定义渲染器中的RippleDrawable
我在Xamarin.Forms中实现了一个自定义的可点击标签类以及一个自定义的渲染器,它添加了一个Xamarin.forms 与父控件的交互会触发Xamarin.Forns自定义渲染器中的RippleDrawable,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,我在Xamarin.Forms中实现了一个自定义的可点击标签类以及一个自定义的渲染器,它添加了一个RippleDrawable作为控件前台。我正在使用以下代码创建RippleDrawable: public static Drawable CreateRippleDrawable(Context context) { var typedValue = new TypedValue(); context.Theme.ResolveAttribute(Resource.Attribu
RippleDrawable
作为控件前台
。我正在使用以下代码创建RippleDrawable
:
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);
并在用户触摸控件时更新涟漪
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
}
}
现在,无论何时单击控件,都会显示涟漪,这是预期的行为,但如果我触摸(轻触或长按)控件的父级(例如堆栈布局
,网格
或包含标签的任何布局,包括其父级布局
,页面
或视图
)将触发涟漪动画。无论如何,在本例中,仅当实际控件被触摸时,才会调用中的事件处理程序LinkLabelRenderer\u Touch
我可以通过向相应的父级添加一个空的手势识别器来解决这个问题,但我真的不喜欢这个解决方案,因为这只是一种攻击。更糟糕的是,无论何时使用控件,我都必须记住这是一个黑客行为
如何防止触摸家长时显示“RippledRavable”
。结果证明我根本搞错了。订阅Touch
事件不是一个好办法。我必须使控件可点击,并订阅点击事件
this.Control.Clickable = true;
this.Click += LinkLabelRenderer_OnClick;
没有必要像我那样(通过Touch
事件)处理所有RippleTouch
东西,但可以让android为我处理事情 你打算支持Android API 19及以下版本吗?不,但谢谢你,我自己解决了这个问题。当然,如果你把它作为一个答案发布,让其他人知道你的想法会更好did@G.hakim我本来打算这么做的,但还没有时间。