在Xamarin中更改滚动条的颜色,Forms

lvjbypge  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(157)

我需要建议。我正在尝试更改Xamarin窗体中的ScrollBar的颜色。我创建了ScrollBarColorEffect,但不幸的是它不起作用,它报告了此错误。
有人会知道错误在哪里。
第一次
XF滚动条颜色效果. cs

public class ScrollBarColorEffect : RoutingEffect
{
    public ScrollBarColorEffect() : base($"MPlay.{nameof(ScrollBarColorEffect)}"){}
    
    public Color ScrollBarColor { get; set; }
}

Xamarin Android滚动条颜色效果. 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

<ScrollView>
        <ScrollView.Effects>
            <helpers:ScrollBarColorEffect ScrollBarColor="Red"/>
        </ScrollView.Effects>
 </ScrollView>

我尝试创建自己的渲染,请参见此处:https://stackoverflow.com/a/65127686/6473719
不幸的是,我得到了同样的错误

0yycz8jy

0yycz8jy1#

您使用的代码应在API 29之前的版本中运行。在Android 10(API 29)上,存在非SDK接口限制。
您使用的方法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开发者中,建议使用setVerticalScrollbarThumbDrawable代替。https://developer. android. google. cn/reference/android/view/View?hl = zh-cn #setVerticalScrollbarThumbDrawable(android. graphics. drawable. drawable)

hvvq6cgz

hvvq6cgz2#

protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        this.VerticalScrollbarThumbDrawable = Context.GetDrawable(Resource.Drawable.scrollbar_style);
    }

相关问题