VNDocumentCameraViewController ใน Xamarin Forms ค้าง

Sep 19 2020

ฉันต้องการใช้สิ่งใหม่VNDocumentCameraViewControllerจาก iOS 13 ในแอป Xamarin Forms ของฉันด้วยตัวแสดงผลแบบกำหนดเอง มันใช้งานได้ แต่บางครั้งหลังจากผ่านไปสองสามวินาทีภาพตัวอย่างจากกล้องก็ค้างและฉันไม่มีโอกาสทำอะไรกับตัวควบคุมมุมมอง

ในการสร้างข้อผิดพลาดอีกครั้งฉันได้ลดรหัสของฉันเป็นดังต่อไปนี้:

มุมมองที่กำหนดเอง:

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>

ตัวแสดงผลที่กำหนดเอง

[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);
            }
        }
    }
}

ส่วนใหญ่เกิดขึ้นเมื่อเคลื่อนกล้องอย่างรวดเร็วจากซ้ายไปขวาและย้อนกลับ แต่บางครั้งก็ไม่ได้ทำอะไรเลย

ฉันไม่พบใครที่พยายามใช้VNDocumentCameraViewControllerกับ Xamarin Forms ฉันทำอะไรผิด? หรือมีจุดบกพร่องหรือไม่

คำตอบ

2 StephanEngelhardt Sep 18 2020 at 22:33

ฉันได้พบวิธีการแก้ปัญหา ... ฉันพยายามที่สองวันที่มันและตอนนี้ผมพบว่าการเก็บขยะได้ F *** งานของเขาและทำลายของฉันscannerControllerหลังจากที่บางเวลา / เรียกของDispose() VNDocumentCameraViewControllerถ้าฉันเปลี่ยนเป็นสมาชิกชั้นเรียนมันได้ผล:

ตัวแสดงผลที่กำหนดเอง

[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);
            }
        }
    }
}