Jon
Jon

Reputation: 190

UWP BarcodeScanner DataRecieved event not firing in WPF app

I have a WPF point of sale app which I have recently ported from net48 to netcore3.1 (what a job!). In the app, I use a Honeywell Voyager 1200g via a POS4NET service object (HHSO4NET.dll) for scanning simple barcodes and all was well. However In the new netcore3.1 world, I could no longer open the device. I got the following error:-

The Type initializer for `Microsoft.Pointofservice.management.Explorer

Method not found: 'Void System.AppDomainSetup.set_ApplicationBase(System.String)'.

I presume that there is something now not available in dotnet core runtime that used to be there in the net framework. So, I decided to look to Windows.Devices.PointOfService in the UWP world to help me integrate the scanner (which is a supported model).

In order to be able to reference these UWP libraries, I followed the following guide which describes adding some additional references

https://blogs.windows.com/windowsdeveloper/2017/01/25/calling-windows-10-apis-desktop-application/

Now I can find, claim and open the scanner just fine! But no events seem to be handled. My code is almost identical to the UWP sample: -

string selector = BarcodeScanner.GetDeviceSelector();
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);
var device = deviceCollection.FirstOrDefault();

if (device != null)
{
    barcodeScanner = await BarcodeScanner.FromIdAsync(device.Id);

    if (barcodeScanner != null)
    {
        //after successful creation, claim the scanner for exclusive use
        var claimedBarcodeScanner = await barcodeScanner.ClaimScannerAsync();

        if (claimedBarcodeScanner != null)
        {
            //Subscribe to the events
            claimedBarcodeScanner.ReleaseDeviceRequested += ClaimedBarcodeScanner_ReleaseDeviceRequested;
            claimedBarcodeScanner.DataReceived += WhenScannerDataReceived;
            claimedBarcodeScanner.IsDecodeDataEnabled = true;

            //after successful claim, enable scanner for data events to fire
            await claimedBarcodeScanner.EnableAsync();
        }
        else
        {
            FrameworkDI.Logger.LogErrorSource("Failure to claim barcodeScanner");
        }
    }
    else
    {
        FrameworkDI.Logger.LogErrorSource("No Barcode Scanner Present");
    }
}
else
{
    FrameworkDI.Logger.LogErrorSource("No Barcode Scanner Present");
}
private void WhenScannerDataReceived(object sender, DataEventArgs args)
{
    string symbologyName = BarcodeSymbologies.GetName(args.Report.ScanDataType);
    var scanDataLabelReader = DataReader.FromBuffer(args.Report.ScanDataLabel);
    string barcode = scanDataLabelReader.ReadString(args.Report.ScanDataLabel.Length);
}

With a break point in the handler, I cannot seem to hit the event. I downloaded the UWP sample app and ran it using the same machine/scanner and it captured all the events and read the data just fine, so I assume that the scanner is emitting events. It must be something to do with the WPF app not getting the events in the same way that the UWP app did somehow.

Is there something I'm missing here?

Upvotes: 0

Views: 752

Answers (2)

kunif
kunif

Reputation: 4350

Rewritten based on the dialogue in comments:

It seems that the following comments attached to the question article were valid.


By the way, does this(Windows.Devices.PointOfService) work naturally with .NET 4.8 WPF(never UWP) instead of .NET Core? If it doesn't work with .NET 4.8 WPF, it probably doesn't work in .NET Core. It would be better to investigate little by little through such small changes.

Maybe it's just reproducing the same situation. But it makes sense to try it with a well-written WPF application, rather than a console application.

That's probably because you didn't do claimedBarcodeScanner.IsDecodeDataEnabled = true; in the DataReceived event handler. Data is buffered in the barcode scanner service while it is not true. That would be the same mechanism as the DataEventEnabled property of POS for.NET and OPOS.


p.s.
Please note that the comments for the previous article will be deleted.


On the other hand, the NuGet package mentioned in the question article is made by a third party and finalized in November 2016, so it may be better to switch to this Microsoft product.

Windows 10 WinRT API Packs released
Microsoft.Windows.SDK.Contracts 10.0.19041.1
Version adaptive code
Windows Runtime APIs available to desktop apps

And .NET 5 will be released this fall or winter, and the mechanism will change.
Support WinRT APIs in .NET 5 #35318
C#/WinRT

Upvotes: 0

Jon
Jon

Reputation: 190

A very basic programming error.

In copying the UWP sample code, I had not realised that I has scoped the claimedBarcodeScanner object locally to an Initialise method.

After the Initialise method had completed, the reference was lost and no events were being captured.

Thanks to kunif for the important advice: "it makes sense to try it with a well-written WPF application, rather than a console application"

...and I can confirm, this POS library works with net framework, net core framework and UWP.

Upvotes: 0

Related Questions