afriscic / BarcodeScanning.Native.Maui

Barcode scanning library for .NET MAUI
https://www.nuget.org/packages/BarcodeScanning.Native.Maui
MIT License
162 stars 31 forks source link

MVVM: obtaining a result #47

Closed Olaf-R closed 5 months ago

Olaf-R commented 5 months ago

Hello again,

how do I retrieve a scanned code from within OnDetectionFinishedCommand? There isn't any bindable BarcodeResult property.

Am I missing something?

Cheers, Olaf

fryndorfer commented 5 months ago

The OnDetectionFinished command returns a result of type BarcodeResult[].

So in your view model, just add the parameter to your command like so:

[RelayCommand]
private void OnDetectionFinished(BarcodeResult[] results)
{
    if (results.Length == 0)
        return;

    // your code...
}

Please note: in one of the newest versions, the type of the result changed from HashSet<BarcodeResult> to BarcodeResult[]. I don't know the exact version that changed it. So depending on your current version of the library, the type of the result may vary.

Olaf-R commented 5 months ago

Cheers, works!

I suppose I really should've found the OnDetectionFinished EventHandler in which the array is defined ...

@afriscic: I'd recommend to add @fryndorfer's sample code to the docs!

afriscic commented 5 months ago

Hello.

@fryndorfer thank you for an answer! @Olaf-R Sorry, unfortunately I'm quite busy this days so i was a bit slow to answer. I'll put @fryndorfer answer into readme in next release.