首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CommunityToolkit.Maui.Core.Views.MauiPopup在Close()上抛出System.ObjectDisposedException

CommunityToolkit.Maui.Core.Views.MauiPopup在Close()上抛出System.ObjectDisposedException
EN

Stack Overflow用户
提问于 2022-09-27 21:00:31
回答 1查看 148关注 0票数 0

当条形码被System.ObjectDisposedException检测到时,下面的代码会在BarcodeScannerPopup.xaml.cs中的行Close(result);上产生一个CameraBarcodeReaderView

检测到的条形码在BarcodePage中的标签中正确显示。

知道为什么会抛出这个异常吗?

BarcodePage.xaml

代码语言:javascript
运行
复制
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage
        x:Class="MyApp.BarcodePage"
        x:DataType="vm:BarcodeViewModel"
        xmlns:vm="clr-namespace:MyApp.ViewModels"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui">
    
        <VerticalStackLayout
            Margin="20">
    
            <Label
                Text="{Binding Barcode}"/>
            <Button
                Command="{Binding ScanBarcodeClickCommand}"
                Text="Scan barcode"/>
        </VerticalStackLayout>
    </ContentPage>

BarcodePage.xaml.cs

代码语言:javascript
运行
复制
    using MyApp.ViewModels;

    namespace MyApp.Views;
    
    public partial class BarcodePage : ContentPage
    {
        public BarcodePage(BarcodeViewModel viewModel)
        {
            InitializeComponent();
            BindingContext = viewModel;
        }
    }

BarcodeScannerPopup.xaml

代码语言:javascript
运行
复制
    <?xml version="1.0" encoding="utf-8" ?>
    <toolkit:Popup
        x:Class="MyApp.BarcodeScannerPopup"
        xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI"
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui">
    
        <VerticalStackLayout
            Margin="20">
    
            <Label
                Text="Add a barcode by scanning it."
                VerticalOptions="Center"
                HorizontalOptions="Center"/>
    
            <zxing:CameraBarcodeReaderView
                x:Name="barcodeReader"
                BarcodesDetected="BarcodesDetected"
                HeightRequest="300"
                IsDetecting="True"
                Margin="5"
                WidthRequest="300"/>
    
        </VerticalStackLayout>
    </toolkit:Popup>

BarcodeScannerPopup.xaml.cs

代码语言:javascript
运行
复制
    using CommunityToolkit.Maui.Views;
    using ZXing.Net.Maui;
    
    namespace MyApp.Views;
    
    public partial class BarcodeScannerPopup : Popup
    {
        public BarcodeScannerPopup()
        {
            InitializeComponent();
    
            barcodeReader.Options = new BarcodeReaderOptions
            {
                AutoRotate = true,
                Multiple = false
            };
        }
    
        private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
        {
            var result = e.Results[0].Value;
    
            Close(result);
        }
    }

BarcodeViewModel.cs

代码语言:javascript
运行
复制
    using CommunityToolkit.Maui.Views;
    using CommunityToolkit.Mvvm.ComponentModel;
    using CommunityToolkit.Mvvm.Input;
    using MyApp.Views;
    
    namespace MyApp.ViewModels;

    public partial class BarcodeViewModel : ObservableObject
    {
        [ObservableProperty]
        private string _barcode;
    
        private BarcodeScannerPopup _popup = new BarcodeScannerPopup();
    
        [RelayCommand]
        public async Task ScanBarcodeClick()
        {
            Barcode = (string)await Application.Current.MainPage.ShowPopupAsync(_popup);
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-28 05:01:58

@ToolmakerSteve是对的。CameraBarcodeReaderView继续发送数据,这将导致System.ObjectDisposedException。将IsDetecting属性设置为false可以防止这种情况发生。由于这是一个PoC,我没有在e上添加一个适当的空检查,所以我在这个示例中添加了一个很好的编码实践。

BarcodeScannerPopup.xaml.cs

代码语言:javascript
运行
复制
    using CommunityToolkit.Maui.Views;
    using ZXing.Net.Maui;
    
    namespace MyApp.Views;
    
    public partial class BarcodeScannerPopup : Popup
    {
        public BarcodeScannerPopup()
        {
            InitializeComponent();
    
            barcodeReader.Options = new BarcodeReaderOptions
            {
                AutoRotate = true,
                Multiple = false
            };
        }
    
        private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
        {
            var result = e?.Results?.Any() == true
                ? e.Results[0].Value
                : string.Empty;
    
            IsDetecting = false;

            Close(result);
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73873760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档