You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Xamarin.Forms简单自定义渲染器迁移至.NET MAUI?

.NET MAUI Android端自定义ScrollView Handler实现滚动控制

步骤1:定义共享自定义ScrollView控件

在共享项目中创建带滚动控制属性的自定义控件:

public class MyScrollView : ScrollView
{
    public static readonly BindableProperty ScrollingEnabledProperty =
        BindableProperty.Create(nameof(ScrollingEnabled), typeof(bool), typeof(MyScrollView), defaultValue: true);

    public bool ScrollingEnabled
    {
        get => (bool)GetValue(ScrollingEnabledProperty);
        set => SetValue(ScrollingEnabledProperty, value);
    }
}

步骤2:创建Android平台自定义Handler及PlatformView

自定义Android ScrollView实现

在Android平台项目中,继承MauiScrollView并重写触摸事件:

using Android.Content;
using Android.Views;
using Microsoft.Maui.Controls.Platform;

namespace YourAppNamespace.Platforms.Android
{
    public class CustomAndroidScrollView : MauiScrollView
    {
        public CustomAndroidScrollView(Context context) : base(context)
        {
        }

        public bool ScrollingEnabled { get; set; } = true;

        public override bool OnInterceptTouchEvent(MotionEvent ev)
        {
            return ScrollingEnabled ? base.OnInterceptTouchEvent(ev) : false;
        }

        public override bool OnTouchEvent(MotionEvent ev)
        {
            return ScrollingEnabled ? base.OnTouchEvent(ev) : false;
        }
    }
}

对应Handler实现

创建Handler替换默认PlatformView,并处理属性同步:

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Controls.Platform;

namespace YourAppNamespace.Platforms.Android
{
    public class MyScrollViewHandler : ScrollViewHandler
    {
        protected override MauiScrollView CreatePlatformView()
        {
            return new CustomAndroidScrollView(Context);
        }

        protected override void ConnectHandler(CustomAndroidScrollView platformView)
        {
            base.ConnectHandler(platformView);
            UpdateScrollingEnabled();
        }

        protected override void DisconnectHandler(CustomAndroidScrollView platformView)
        {
            platformView.Dispose();
            base.DisconnectHandler(platformView);
        }

        public static void MapScrollingEnabled(MyScrollViewHandler handler, MyScrollView scrollView)
        {
            handler.UpdateScrollingEnabled();
        }

        private void UpdateScrollingEnabled()
        {
            if (PlatformView is CustomAndroidScrollView customScrollView && VirtualView is MyScrollView myScrollView)
            {
                customScrollView.ScrollingEnabled = myScrollView.ScrollingEnabled;
            }
        }
    }
}

步骤3:注册Handler

在MauiProgram.cs中注册自定义Handler:

using Microsoft.Maui.Hosting;
using YourAppNamespace.Platforms.Android;

namespace YourAppNamespace
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureMauiHandlers(handlers =>
                {
#if ANDROID
                    handlers.AddHandler(typeof(MyScrollView), typeof(MyScrollViewHandler));
#endif
                });

            return builder.Build();
        }
    }
}

步骤4:iOS端适配(补充)

iOS端直接通过Handler设置原生属性即可:

// iOS平台项目中的Handler
using Microsoft.Maui.Handlers;
using UIKit;

namespace YourAppNamespace.Platforms.iOS
{
    public class MyScrollViewHandler : ScrollViewHandler
    {
        public static void MapScrollingEnabled(MyScrollViewHandler handler, MyScrollView scrollView)
        {
            handler.PlatformView.ScrollEnabled = scrollView.ScrollingEnabled;
        }
    }
}

在MauiProgram.cs中添加iOS的Handler注册:

// 在ConfigureMauiHandlers中
#if IOS
handlers.AddHandler(typeof(MyScrollView), typeof(MyScrollViewHandler));
#endif

使用方式

在XAML或代码中直接使用自定义控件,通过ScrollingEnabled属性控制滚动:

<local:MyScrollView ScrollingEnabled="{Binding IsDrawing}">
    <!-- 内容区域 -->
</local:MyScrollView>

内容的提问来源于stack exchange,提问作者Derlidio Siqueira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:26:09