Customize controls with .NET MAUI using PlatformViewFactory

I am an enthusiastic software developer focusing on C#, .NET, Asp.NET, Azure and Angular.
Search for a command to run...

I am an enthusiastic software developer focusing on C#, .NET, Asp.NET, Azure and Angular.
No comments yet. Be the first to comment.
gRPC, a high-performance, open-source RPC (Remote Procedure Call) framework developed by Google, has gained immense popularity among developers for building efficient and robust APIs. This blog post will explore how to get started with gRPC services ...

GraphQL is a powerful query language and runtime that efficiently fetches and manipulates data from APIs. This blog post will explore how to get started with GraphQL in Asp.Net Core. What is GraphQL? GraphQL is a query language and runtime for APIs t...

Observability is a key concept in modern software development. It refers to the ability of a system to be monitored, measured, and understood in order to identify and address issues. In the context of ASP.NET Core, observability can help developers a...

Introduction to Blazor Blazor is a web framework from Microsoft that allows developers to build interactive web applications using C# and HTML. With Blazor, we can create web applications that run entirely in the browser without the need for plugins ...

In this previous article, we learned about caching in Asp.Net Core. We talked about In-Memory cache and Distributed cache using SQL Server cache. This article will discuss how to use Redis for caching in Asp.Net Core. What is Redis? Redis (REmote DIc...

In this post we will use PlatformViewFactory to customize a control without creating a new handler.
When I wrote this article about customize controls with handlers, Pedro Jesus replied to my tweet suggesting to reimplement the control using PlatformViewFactory.
It is a static property of ViewHandler that allows to return any implementation of ViewHandler or even the native control itself.
To register it should be added in MauiProgram.cs (or anywhere before Entry handlers will be created in the app).
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
EntryImageHandler.Register();
return builder.Build();
}
}
The difference using PlatformViewFactory is instead of creating a custom handler, the function receives the default EntryHandler allowing us to customize the native control as needed.
public static class EntryImageHandler
{
public static void Register()
{
EntryHandler.PlatformViewFactory = (handler) =>
{
var editText = new AppCompatEditText(handler.Context);
if (handler.VirtualView is not ImageEntry)
return editText;
var element = handler.VirtualView as ImageEntry;
if (!string.IsNullOrEmpty(element.Image))
{
switch (element.ImageAlignment)
{
case ImageAlignment.Left:
editText.SetCompoundDrawablesWithIntrinsicBounds(GetDrawable(element.Image, handler.Context, element), null, null, null);
break;
case ImageAlignment.Right:
editText.SetCompoundDrawablesWithIntrinsicBounds(null, null, GetDrawable(element.Image, handler.Context, element), null);
break;
}
}
editText.CompoundDrawablePadding = 25;
editText.Background.SetColorFilter(Colors.White.ToAndroid(), PorterDuff.Mode.SrcAtop);
return editText;
};
}
}
public static class EntryImageHandler
{
public static void Register()
{
EntryHandler.PlatformViewFactory = (handler) =>
{
var textField = new MauiTextField();
if (handler.VirtualView is not ImageEntry)
return textField;
var element = handler.VirtualView as ImageEntry;
if (!string.IsNullOrEmpty(element.Image))
{
switch (element.ImageAlignment)
{
case ImageAlignment.Left:
textField.LeftViewMode = UITextFieldViewMode.Always;
textField.LeftView = GetImageView(element.Image, element.ImageHeight, element.ImageWidth);
break;
case ImageAlignment.Right:
textField.RightViewMode = UITextFieldViewMode.Always;
textField.RightView = GetImageView(element.Image, element.ImageHeight, element.ImageWidth);
break;
}
}
textField.BorderStyle = UITextBorderStyle.Line;
textField.Layer.MasksToBounds = true;
return textField;
};
}
}
You can find the full code on my GitHub.