Hello everyone,
I'm trying to create a practice application using .NET 9 and MAUI. However, I encounter an issue while implementing the barcode reading functionality.
Within this code:
cameraView.BarCodeOptions = new ZXing.Net.Maui.BarcodeDecodeOptions()
{
TryHarder = true,
PossibleFormats = { ZXing.BarcodeFormat.All_1D }
};
My problem lies in this line:
.BarcodeDecodeOptions()
I receive the following error: CS0234
.
Here is the complete code:
using MauiApp1.DataAccess;
using MauiApp1.DTOs;
using MauiApp1.Models;
using MauiApp1.Utilidades;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.EntityFrameworkCore;
using ZXing.Net.Maui;
namespace MauiApp1.Views;
public partial class EscanearProductoPage : ContentPage
{
private readonly VentaDbContext _context;
public EscanearProductoPage(VentaDbContext context)
{
InitializeComponent();
cameraView.BarCodeOptions = new ZXing.Net.Maui.BarcodeDecodeOptions()
{
TryHarder = true,
PossibleFormats = { ZXing.BarcodeFormat.All_1D }
};
_context = context;
}
private void cameraView_CamerasLoaded(object sender, EventArgs e)
{
if (cameraView.Cameras.Count > 0)
{
cameraView.Camera = cameraView.Cameras.First();
MainThread.BeginInvokeOnMainThread(new Action(async () =>
{
await cameraView.StopCameraAsync();
await cameraView.StartCameraAsync();
}));
}
}
private async void cameraView_BarcodeDetected(object sender, Camera.MAUI.ZXingHelper.BarcodeEventArgs args)
{
MainThread.BeginInvokeOnMainThread(async () =>
{
string codigo = args.Result[0].Text;
Producto dbProducto = await _context.Productos.Include(c => c.RefCategoria).FirstOrDefaultAsync(p => p.Codigo == codigo);
ProductoDTO producto = new ProductoDTO()
{
IdProducto = dbProducto.IdProducto,
Codigo = dbProducto.Codigo,
Nombre = dbProducto.Nombre,
Categoria = new CategoriaDTO()
{
IdCategoria = dbProducto.IdCategoria,
Nombre = dbProducto.RefCategoria.Nombre
},
Cantidad = dbProducto.Cantidad,
Precio = dbProducto.Precio
};
WeakReferenceMessenger.Default.Send(new ProductoVentaMessage(producto));
});
await Shell.Current.Navigation.PopModalAsync();
}
}
In my MauiProgram.cs, I have the following code:
using Microsoft.Extensions.Logging;
using MauiApp1.DataAccess;
using MauiApp1.ViewModels;
using CommunityToolkit.Maui;
using MauiApp1.Views;
using Camera.MAUI;
using ZXing.Net.Maui.Controls;
using ZXing.Net.Maui;
namespace MauiApp1
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.UseMauiCameraView()
.UseBarcodeReader()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("fa-solid-900.ttf", "FaSolid");
});
builder.Services.AddDbContext<VentaDbContext>();
builder.Services.AddTransient<CategoriasPage>();
builder.Services.AddTransient<CategoriasVM>();
builder.Services.AddTransient<InventarioPage>();
builder.Services.AddTransient<InventarioVM>();
builder.Services.AddTransient<ProductoPage>();
builder.Services.AddTransient<ProductoVM>();
builder.Services.AddTransient<VentaPage>();
builder.Services.AddTransient<VentaVM>();
builder.Services.AddTransient<BuscarProductoPage>();
builder.Services.AddTransient<BuscarProductoVM>();
builder.Services.AddTransient<HistoriaVentaPage>();
builder.Services.AddTransient<HistorialVentaVM>();
builder.Services.AddTransient<MainPage>();
builder.Services.AddTransient<MainVM>();
var dbContext = new VentaDbContext();
dbContext.Database.EnsureCreated();
dbContext.Dispose();
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
}
In theEscanearProductoPage.XAML file, I have the following code at the top:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="MauiApp1.Views.EscanearProductoPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
Title="EscanearProductoPage"
BackgroundColor="{StaticResource bgLightGray}">
I would appreciate any guidance on what else I should check. I've reviewed a lot of documentation but haven't been able to resolve the error.
Thank you very much!