[英]Change the color of the ScrollBar in Xamarin.Forms
I need advice.我需要建议。 I'm trying to change the color of the ScrollBar in Xamarin Forms.我正在尝试更改 Xamarin Forms 中 ScrollBar 的颜色。 I created ScrollBarColorEffect but unfortunately it doesn't work it reports this error.我创建了 ScrollBarColorEffect 但不幸的是它不起作用它报告此错误。
Someone would know where the mistake was.有人会知道错误在哪里。
XF ScrollBarColorEffect.cs XF ScrollBarColorEffect.cs
public class ScrollBarColorEffect : RoutingEffect
{
public ScrollBarColorEffect() : base($"MPlay.{nameof(ScrollBarColorEffect)}"){}
public Color ScrollBarColor { get; set; }
}
Xamarin Android ScrollBarColorEffect.cs Xamarin Android ScrollBarColorEffect.cs
[assembly: ResolutionGroupName("MPlay")]
[assembly: ExportEffect(typeof(MPlay.Droid.ScrollBarColorEffect),
nameof(MPlay.Droid.ScrollBarColorEffect))]
namespace MPlay.Droid
{
public class ScrollBarColorEffect : PlatformEffect
{
protected override void OnAttached()
{
UpdateUI();
}
protected override void OnDetached()
{
}
void UpdateUI()
{
Xamarin.Forms.ScrollView _scrollView = Element as Xamarin.Forms.ScrollView;
if (Element != null && Control is AndroidX.Core.Widget.NestedScrollView scrollView)
{
Java.Lang.Reflect.Field mScrollCacheField = Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache");
mScrollCacheField.Accessible = true;
var mScrollCache = mScrollCacheField.Get(scrollView);
var scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
scrollBarField.Accessible = true;
var scrollBar = scrollBarField.Get(mScrollCache);
if (scrollBar != null)
{
var method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Class.FromType(typeof(Drawable)));
method.Accessible = true;
var layers = new Drawable[1];
var shapeDrawable = new ShapeDrawable(new RectShape());
var scrollBarColor = Color.Default;
var effect = _scrollView.Effects.FirstOrDefault(e => e is Core.Effects.ScrollBarColorEffect) as Core.Effects.ScrollBarColorEffect;
//var effect = (Core.Effects.ScrollBarColorEffect)Element.Effects.FirstOrDefault(e => e is Core.Effects.ScrollBarColorEffect);
if (effect != null)
{
scrollBarColor = effect.ScrollBarColor;
}
shapeDrawable.Paint.Color = scrollBarColor.ToAndroid();
shapeDrawable.SetIntrinsicWidth(5);
layers[0] = shapeDrawable;
method.Invoke(scrollBar, layers);
}
}
}
}
XAML XF XAML XF
<ScrollView>
<ScrollView.Effects>
<helpers:ScrollBarColorEffect ScrollBarColor="Red"/>
</ScrollView.Effects>
</ScrollView>
I tried to create my own render, see here: https://stackoverflow.com/a/65127686/6473719我尝试创建自己的渲染,请参见此处: https://stackoverflow.com/a/65127686/6473719
Unfortunately, I get the same error不幸的是,我得到了同样的错误
The code you used should work before API 29. On Android 10(API 29), there are non-SDK interface restrictions.您使用的代码应该在 API 29 之前工作。在 Android 10(API 29) 上,存在非 SDK 接口限制。
The method setVerticalThumbDrawable
you used is in the Non-SDK interfaces that are now blocked in Android 10.您使用的方法setVerticalThumbDrawable
在非 SDK 接口中,现在在 Android 10 中被阻止。
Non-SDK interfaces that are now blocked in Android 10: https://developer.android.google.cn/about/versions/10/non-sdk-q#q-list-changes现在Android 10中被屏蔽的非SDK接口: https://developer.android.google.cn/about/q#q--changes/non-sdk
In the Android developer, it suggest to use setVerticalScrollbarThumbDrawable
instead.在 Android 开发者中,建议改用setVerticalScrollbarThumbDrawable
。 https://developer.android.google.cn/reference/android/view/View?hl=zh-cn#setVerticalScrollbarThumbDrawable(android.graphics.drawable.Drawable) https://developer.android.google.cn/reference/android/view/View?hl=zh-cn#setVerticalScrollbarThumbDrawable(android.graphics.drawable.Drawable)
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.VerticalScrollbarThumbDrawable = Context.GetDrawable(Resource.Drawable.scrollbar_style);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.