Is there a way to render the Zxing ScannerPage in Xaml?

I'm using the MVVM approach to make a small scanner app. I have a startPage.Xaml with one button binded to a command in the viewModel that opens my scannerPage.Xaml On the scannerPage I have just a simple footer, and want the scanner object to fill the rest of the page. However, when I add the scanner object to my stackPanel I get the following exception: "System.MissingMethodException: Default constructor not found for type ZXing.Net.Mobile.Forms.ZXingScannerPage". I'll list my xaml code below. Any help will be much appreciated. Thanks

<?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:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             xmlns:custom="using:MobiNative_Forms.Views"
             x:Class="MobiNative_Forms.Views.BarcodePage"
             BackgroundColor="Transparent">
  <ContentPage.Content>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="8.5*" />
        <RowDefinition Height="1.5*" />
      </Grid.RowDefinitions>
      <StackLayout x:Name="layout2" Grid.Row="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <forms:ZXingScannerPage></forms:ZXingScannerPage>
      </StackLayout>
      <Label Grid.Row="1" Text="MOBI" FontSize="Large" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="Blue"/>
    </Grid>
  </ContentPage.Content>
</ContentPage>

Answers

  • KellyHussKellyHuss USMember ✭✭

    The ZXingScannerPage is a page, not a view. Maybe try ZXingScannerView instead?

  • KrzysztofKazmierczakKrzysztofKazmierczak USMember ✭✭
    edited July 14

    Hi! The thread is quite old, but just in case - this is the xaml which works:

    <?xml version="1.0" encoding="utf-8" ?>

        <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <forms:ZXingScannerView IsScanning="{Binding IsScanning}" IsAnalyzing="{Binding IsAnalyzing}" Result="{Binding Result, Mode=TwoWay}" ScanResultCommand="{Binding QRScanResultCommand}"></forms:ZXingScannerView>
            <forms:ZXingDefaultOverlay TopText="Sample top text" BottomText="sample bottom text" ShowFlashButton="False" Opacity="0.9"></forms:ZXingDefaultOverlay>
        </Grid>
    

    Best regards!

  • DougMaurerDougMaurer USMember

    @KrzysztofKazmierczak , I am using your suggestions but am confused on where to put the events for the actual scanning. I am using an mvvm model (prism) so I have a viewmodel set for this page. What would the events look like in that model to be able to receive the scan, etc.?

  • And viewmodel code just in case:

    public class QRCodeScannerViewModel
    {
        public ZXing.Result Result { get; set; }
    
        private bool isAnalyzing = true;
        public bool IsAnalyzing
        {
            get { return this.isAnalyzing; }
            set
            {
                if (!bool.Equals(this.isAnalyzing, value))
                {
                    this.isAnalyzing = value;
                    this.OnPropertyChanged(nameof(IsAnalyzing));
                }
            }
        }
    
        private bool isScanning = true;
        public bool IsScanning
        {
            get { return this.isScanning; }
            set
            {
                if (!bool.Equals(this.isScanning, value))
                {
                    this.isScanning = value;
                    this.OnPropertyChanged(nameof(IsScanning));
                }
            }
        }
    
        public Command QRScanResultCommand
        {
            get
            {
                return new Command(() =>
                {
                    IsAnalyzing = false;
                    IsScanning = false;
    
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        //do your job here - Result.Text contains QR CODE
                    });
                });
            }
        }
    }
    

    Best regards!
    Krzysztof

  • DougMaurerDougMaurer USMember

    Awesome! I got my app working!

    But, alas, a follow-up situation. After 1 scan, the scanner window is stuck on the last image. How do I get the scanner to scan an item, update an entry on screen (for example), and then get right back to scanning?

Sign In or Register to comment.