在 Xamarin.Forms 中,我能够通过使用 PageRenderer 子类化内容页面来重写 OnFilterTouchEventForSecurity()。我想在 MAUI 中实现相同的目标,但当我使用 PageHandler 子类化内容页面时,我无法找到此覆盖方法。谁能帮我解决这个问题吗?
Xamarin 表单:
public override bool OnFilterTouchEventForSecurity(MotionEvent e)
{ }
.NET 毛伊岛: 我可以在 ConnectHandler 中作为 platformView.OnFilterTouchEventForSecurity 访问它,但我不确定如何在这里使用它。
protected override void ConnectHandler(ContentViewGroup platformView)
{
base.ConnectHandler(platformView);
}
您可以参考以下代码:
创建自定义页面:
public class TestPage : ViewGroup
{
public TestPage (Context? context) : base (context) {}
public override boolOnFilterTouchEventForSecurity (MotionEvent? e)
{
return base.OnFilterTouchEventForSecurity (e);
}
protected override void OnLayout (bool changed, int l, int t, int r, int b)
{
//You need to modify your layout
var content = GetChildAt (0);
if (content?.Visibility != ViewStates.Gone)
{
content?.Layout (0, 0, content.MeasuredWidth, content.MeasuredHeight);
}
//....
}
}
声明一个处理程序:
public class MyPageHandler : ViewHandler<ContentPage, TestPage>
{
public static IPropertyMapper<ContentPage, MyPageHandler> PropertyMapper =
new PropertyMapper<ContentPage, MyPageHandler>(ViewHandler.ViewMapper);
public MyPageHandler() : base(PropertyMapper)
{
}
protected override void ConnectHandler(TestPage platformView)
{
base.ConnectHandler(platformView);
}
protected override TestPage CreatePlatformView() => new TestPage(Context);
}
将其添加到Maui程序中:
{
public static MauiApp
CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts (fonts => {
fonts.AddFont ("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont ("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers (handlers => {
#if ANDROID
handlers.AddHandler<ContentPage, MyPageHandler>();
#endif
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
这是。但这需要您设置布局。您可以检查此案例:在自定义 ViewGroup 中使用 onLayout 正确布局子项。