-1

I am currently migrating my Xamarin.Forms app to .NET MAUI, and having a difficulty in migrating view renderer. In .NET MAUI I am using camera2 in my app, and using the renderer for same. My Xamarin forms code is

    public class CameraRecordV3 : View
    {
        public static readonly BindableProperty StartProperty = BindableProperty.Create(
            "Start", typeof(int), typeof(int), 6000);

        public int Start
        {
            set { SetValue(StartProperty, value); }
            get { return (int)GetValue(StartProperty); }
        }
}
    using iVue.Views;
    using System.ComponentModel;
    using Microsoft.Maui.Controls.Platform;
    using Microsoft.Maui.Controls.Handlers.Compatibility;
    
    namespace iVue.Platforms.Android.Renderers;
    
    public class CameraRecordRenderer_V3 : ViewRenderer<CameraRecordV3, CameraRecordControl_V3>
    {
        private CameraRecordControl_V3 _cameraControl;
        private DisplayTimeHelper _displayTimeHelper = new DisplayTimeHelper();
    
        public CameraRecordRenderer_V3(Context context)
          : base(context)
        {
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<CameraRecordV3> e)
        {
            base.OnElementChanged(e);
    
            if (Control == null)
            {
                _cameraControl = new CameraRecordControl_V3(Context, e.NewElement);
                SetNativeControl(_cameraControl);
            }
        }
    
     protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var model = (CameraRecordV3)sender;
            base.OnElementPropertyChanged(sender, e);
        }
    
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                _cameraControl.Dispose();
                if(Control != null)
                    Control.Dispose();
            }
        }
    }

CameraRecordControl_V3 is a viewgroup which contains a native view for android, which contains buttons and camera

    public class CameraRecordControl_V3 : ViewGroup
    {
        public CameraRecordControl_V3(Context context, CameraRecordV3 vm) : base(context)
        {
            _activity = this.Context as Activity;
            _view = _activity.LayoutInflater.Inflate(Resource.Layout.CameraRecordLayoutV2, this, false);
            AddView(_view);

            _toolbar = (Toolbar)_view.FindViewById(Resource.Id.toolbar);
            textureView = (AutoFitTextureView)_view.FindViewById(Resource.Id.textureview)
            _questionTitleView = (Button)_view.FindViewById(Resource.Id.Start);
}
}

I tried using handler in .net maui but no luck with it.

My Maui Code is as follows

public interface ICameraRecordV3 : IView 
{   
    public int StartTime { get; }
}

public partial class CameraRecordV3Handler
    {
        public static PropertyMapper<ICameraRecordV3, CameraRecordV3Handler> CustomMapper 
            = new PropertyMapper<ICameraRecordV3, CameraRecordV3Handler>(ViewHandler.ViewMapper)
        {
            [nameof(ICameraRecordV3.StartTime)] = MapStartTime,
        };

        public CameraRecordV3Handler() : base(CustomMapper)
        {

        }

        public CameraRecordV3Handler(PropertyMapper mapper = null) : base(mapper ?? CustomMapper)
        {

        }
    }



public class CameraRecordV3 : View, ICameraRecordV3 
    {
        public static readonly BindableProperty StartProperty = BindableProperty.Create(
            "StartTime", typeof(int), typeof(int), 6000);

        public int Start
        {
            set { SetValue(StartTimeProperty, value); }
            get { return (int)GetValue(StartTimeProperty); }
        }
}

//Platform Specific code

public partial class CameraRecordV3Handler : ViewHandler<ICameraRecordV3, CameraRecordControl_V3>
    {
        private CameraRecordControl_V3 _cameraControl;
        protected override CameraRecordControl_V3 CreatePlatformView()
        {
            _cameraControl = new CameraRecordControl_V3(Context, null);
            return _cameraControl;
        }

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

        private static void MapStartTime(CameraRecordV3Handler handler, ICameraRecordV3 arg2)
        {
            handler.PlatformView?.UpdateStartTime(arg2.StartTime);
        }
}

//MauiProgram

builder.ConfigureMauiHandlers(handlers =>
        {
            #if __ANDROID__
            handlers.AddHandler(typeof(CameraRecordV3), typeof(iVue.Handlers.CameraRecordV3Handler));
            #endif
        });
  • 1
    What do you mean by, "I tried using handler in .net maui but no luck with it."? You will have to be specific about what issue are you facing otherwise your question will either keep getting downvoted and probably get closed – FreakyAli Sep 28 '22 at 12:55
  • I am migrating my xamarin.forms app to maui, in my app I have camera feature which is implemented using viewrenderers for android and ios. I tried converting CameraRecordRenderer_V3 VIEWRENDERER to VIEWHANDLER as per documentation specified here https://github.com/dotnet/maui/wiki/Porting-Custom-Renderers-to-Handlers but it didnt work – Sonali Rane Sep 29 '22 at 04:34
  • Can I see your Maui code so far? – FreakyAli Sep 29 '22 at 05:33
  • @FreakyAli thankyou for your reply :), I have updated my question and added my maui code in it, please check the same question – Sonali Rane Sep 29 '22 at 06:20
  • Note: I have not changed anything in CameraRecordControl_V3 **viewgroup** – Sonali Rane Sep 29 '22 at 06:37

1 Answers1

0

You can continue to use CustomRenderer in MAUI, you just need to Remove any ExportRenderer directives as they won't be needed in .NET MAUI. And then configure each renderer using conditional compilation for each platform. You can replace handlers.AddCompatibilityRenderer with handlers.AddHandler in the documentation. Using handlers.AddCompatibilityRenderer will cause a crash.

Zack
  • 1,255
  • 1
  • 2
  • 5
  • I did the same thing but getting the blank page instead – Sonali Rane Sep 29 '22 at 10:54
  • can you provide me the example of viewrenderer using native views – Sonali Rane Sep 29 '22 at 12:30
  • You can try some simple renderer to see if it works, for example changing the background color. – Zack Sep 30 '22 at 01:29
  • yes, I tried putting only relative layout and changing its background in native view(Path to the nativeview => Platform/Android/Resource/layout/nativeview.axml) but didnt do anything – Sonali Rane Sep 30 '22 at 05:02
  • Can you please share me a sample code so that it help me out – Sonali Rane Sep 30 '22 at 05:48
  • This issue's answer case can help you:https://stackoverflow.com/questions/72404629/maui-customize-an-entry#:~:text=These%20are%20the,AddHandler%20in%20MauiProgram. – Zack Sep 30 '22 at 08:17
  • I am working on camera2, the page contains AutoFitTextureView, I am able to see the buttons but not the camera – Sonali Rane Oct 03 '22 at 07:17
  • The Maui class library is divided into a platform-specific part and a public part. You need to be on a specific platform to use camera2.https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/single-project#platform-specific-code – Zack Oct 06 '22 at 06:15