You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI中ZXing.Net.Maui.Controls摄像头无法隐藏问题求助

解决ZXing.Net.Maui CameraBarcodeReaderView无法隐藏的问题

问题分析

直接设置CameraBarcodeReaderView.IsVisible = false无法隐藏摄像头预览,是因为控件底层的摄像头捕获会话没有随可见性变更立即停止,导致画面仍会持续显示。需要先主动停止摄像头捕获再控制控件可见性,或者通过包裹容器的方式间接隐藏摄像头区域。


解决方案1:主动控制摄像头生命周期

通过调用StartCameraAsync和StopCameraAsync方法,配合IsDetecting和IsVisible属性,完整控制摄像头的启停与显示状态:

修改后的MainPage.xaml.cs

namespace Test
{
    public partial class MainPage : ContentPage
    {       
        public MainPage()
        {
            InitializeComponent();
        }

        private async void BtSh_Clicked(object sender, EventArgs e)
        {
            // 显示摄像头并重启捕获
            BcScaner.IsVisible = true;
            await BcScaner.StartCameraAsync();
            BcScaner.IsDetecting = true;
            BtSh.IsVisible = false;
        }

        private async void CameraBarcodeReaderView_BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e)
        {
            try
            {
                // 立即停止检测,避免重复触发事件
                BcScaner.IsDetecting = false;
                // 停止摄像头捕获会话
                await BcScaner.StopCameraAsync();

                // 在UI线程统一更新控件状态
                await Dispatcher.DispatchAsync(async () =>
                {
                    BcScaner.IsVisible = false;
                    BtSh.IsVisible = true;
                    await DisplayAlert("扫描成功", e.Results[0].Value.ToString(), "确定");
                });
            }
            catch (Exception ex)
            {
                // 可添加错误日志记录
            }
        }
    }
}

解决方案2:使用容器包裹摄像头控件

通过将CameraBarcodeReaderView放入一个容器(如ContentView),隐藏容器而非直接隐藏摄像头控件,这种方式更可靠:

修改后的MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
             x:Class="Test.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">

            <!-- 用ContentView包裹摄像头控件,控制容器的可见性 -->
            <ContentView x:Name="CameraContainer" IsVisible="True">
                <zxing:CameraBarcodeReaderView       
                    IsTorchOn="False"                                      
                    BarcodesDetected="CameraBarcodeReaderView_BarcodesDetected"
                    x:Name="BcScaner"
                    CameraLocation="Rear"/>
            </ContentView>

            <Button
                x:Name="BtSh"
                Text="下次扫描"
                Clicked="BtSh_Clicked"
                IsVisible="False"/>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

修改后的MainPage.xaml.cs

namespace Test
{
    public partial class MainPage : ContentPage
    {       
        public MainPage()
        {
            InitializeComponent();
        }

        private void BtSh_Clicked(object sender, EventArgs e)
        {
            CameraContainer.IsVisible = true;
            BcScaner.IsDetecting = true;
            BtSh.IsVisible = false;
        }

        private async void CameraBarcodeReaderView_BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e)
        {
            try
            {
                BcScaner.IsDetecting = false;

                await Dispatcher.DispatchAsync(async () =>
                {
                    CameraContainer.IsVisible = false;
                    BtSh.IsVisible = true;
                    await DisplayAlert("扫描成功", e.Results[0].Value.ToString(), "确定");
                });
            }
            catch (Exception ex)
            {
                // 可添加错误日志记录
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Sphinx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:54:52