简体   繁体   中英

VNDocumentCameraViewController in Xamarin Forms freezes

I would like to use the new VNDocumentCameraViewController from iOS 13 in my Xamarin Forms App with a custom renderer. It works, but sometimes after a few seconds the preview from the camera freezes and I have no chance to do anything on the view controller.

To reproduce the error, I've reduced my code to the following:

Custom view:

public sealed class Scanner : View
{
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage">
    <local:Scanner />
</ContentPage>

Custom renderer

[assembly: ExportRenderer(typeof(App1.Scanner), typeof(App1.iOS.ScannerRenderer))]

namespace App1.iOS
{
    public class ScannerRenderer : ViewRenderer<Scanner, UIView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Scanner> e)
        {
            base.OnElementChanged(e);

            if (this.Control == null)
            {
                VNDocumentCameraViewController scannerController = new VNDocumentCameraViewController();
                this.SetNativeControl(scannerController.View);
            }
        }
    }
}

It mostly occurs when moving the camera fast from left to right and back, but sometimes also without doing anything.

I didn't found anyone who tries to use the VNDocumentCameraViewController with Xamarin Forms. What I'm doing wrong? Or is there a bug?

I've found the solution...I struggled two days on it and now I found out, that the garbage collector did his f*** job and destroyed my scannerController after some time / called Dispose() of VNDocumentCameraViewController . If I changed it to a class member it worked:

Custom renderer

[assembly: ExportRenderer(typeof(App1.Scanner), typeof(App1.iOS.ScannerRenderer))]

namespace App1.iOS
{
    public class ScannerRenderer : ViewRenderer<Scanner, UIView>
    {
        private VNDocumentCameraViewController scannerController;
    
        protected override void OnElementChanged(ElementChangedEventArgs<Scanner> e)
        {
            base.OnElementChanged(e);

            if (this.Control == null)
            {
                this.scannerController = new VNDocumentCameraViewController();
                this.SetNativeControl(this.scannerController.View);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM