maui icon indicating copy to clipboard operation
maui copied to clipboard

Custom Renderers just don't work. At all.

Open BlueRaja opened this issue 1 year ago • 11 comments

Description

If I create an empty custom renderer for a default MAUI control, I would expect it to have no effect, because it's not doing anything. Instead it prevents the entire control from rendering.

Steps to Reproduce

  1. Create an empty custom Switch renderer
using Android.Content;
using Microsoft.Maui.Controls.Handlers.Compatibility;

namespace MyApp.Renderers
{
    internal class MySwitchRenderer: VisualElementRenderer<Switch>
    {
        public MySwitchRenderer(Context context) : base(context) { }
    }
}
  1. Configure it in MauiProgram.cs
public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        ...
        .ConfigureMauiHandlers(() => handlers.AddHandler(typeof(Switch), typeof(MyApp.Renderers.MySwitchRenderer)));

    return builder.Build();
}
  1. Display a switch in the app
<Switch />

Expected result: Switch is displayed

Actual result: Switch is not displayed

The same thing happens with Picker, and I assume others.

Link to public reproduction project repository

No response

Version with bug

8.0.3 GA

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

Nope. Handlers don't support many real-world use-cases (example) so they are not a valid workaround.

Relevant log output

No response

BlueRaja avatar Mar 06 '24 03:03 BlueRaja

In MauiProgram.cs instead of using AddHandler try using AddCompatibilityRenderer.

czuck avatar Mar 06 '24 17:03 czuck

try using AddCompatibilityRenderer

After:

  • Adding .UseMauiCompatibility() to the MauiApp Builder
  • Changing VisualElementRenderer<Switch> to SwitchRenderer

this works, in the sense that it no longer crashes and the code is called (though renderers that used to work in Xamarin are still not working in Maui). However, VS complains that SwitchRenderer is deprecated. There's also no mention of this in the documentation.

According to the documentation, it needs to be VisualElementRenderer<Switch> + AddHandler. Is the documentation just completely wrong? Or is this bug report still valid?

BlueRaja avatar Mar 07 '24 03:03 BlueRaja

What I'm more interested in is your statement about handlers not being useful rather than reusing renderers which should not be a long-term solution.

Why do you think that? What is it about this Switch that you cannot do with a handler according to you?

jfversluis avatar Mar 07 '24 07:03 jfversluis

I gave an example in the post already.

BlueRaja avatar Mar 07 '24 08:03 BlueRaja

Take a look at this and see if it is helpful

czuck avatar Mar 07 '24 21:03 czuck

@czuck Yes, that is the example I just mentioned. It's also my post.

BlueRaja avatar Mar 08 '24 00:03 BlueRaja

Hi @BlueRaja. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

Yes the issue still exists on 8.0.7

BlueRaja avatar Mar 22 '24 00:03 BlueRaja

We experience the same issue with 8.0.20. Trying to add a Custom Renderer will prevent the rendering of the control and it's children.

phillippschmedt avatar Apr 12 '24 10:04 phillippschmedt

I've uploaded an example of a non-working iOS renderer (which is completely empty) with MAUI here.

Once the renderer is being used (in this example it's for Grid), the content inside the Grid is not shown.

Custom renderers are supposed to work in MAUI until .NET9 arrives, right?

Zwetschgenfleck avatar Apr 12 '24 11:04 Zwetschgenfleck

Verified this issue with Visual Studio 17.10.0 Preview 3(8.0.20/8.0.3). Can repro this issue whit the sample project in the latest comment.

Zhanglirong-Winnie avatar Apr 18 '24 02:04 Zhanglirong-Winnie