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

Issue when OnDetectionFinishedCommand="DetectionFinishedCommand" #57

Closed NorMoll closed 4 months ago

NorMoll commented 4 months ago

Firstly thank you for some great work done.

When using the recommended OnDetectionFinishedCommand="DetectionFinishedCommand" produces: Error XFC0009 No property, BindableProperty, or event found for "OnDetectionFinishedCommand", or mismatching type between value and property.

Changing the xaml to OnDetectionFinishedCommand="{Binding DetectionFinishedCommand}" removes the Error but the

DetectionFinishedCommand is never executed (may be due to a previous issue reported today)

Also seems it should be done like this: DetectionFinishedCommand = new Command<BarcodeResult[]>(result => { if (result.Length > 0) { // } }

Olaf-R commented 4 months ago

To avoid misunderstandings: are you using the MVVM toolkit? If so, it should look like this in your XAML:

       <scanner:CameraView 
                           BarcodeSymbologies="QRCode,Code39"
                           OnDetectionFinishedCommand="{Binding BarcodeScannedCommand}"
                           .../>

... and like this in your VM:

    [RelayCommand]
    public async Task BarcodeScanned(BarcodeResult[] results)
    {
    ...
    }
afriscic commented 4 months ago

Oh I've noticed now that the example in the README is incorrect. Use @Olaf-R example and it should work normally. If you don't use MVVM toolkit use this example for your Command:

    public ICommand DetectionFinishedCommand { get; set; }
    ...
    DetectionFinishedCommand = new Command<BarcodeResult[]>(BarcodeResult[] result) =>
    {
        if (result.Length > 0)
            ...
    }
NorMoll commented 4 months ago

Thank you for the responses.

For reference; I downloaded the project (So no MVVM added) and in the ...Test Project simply removed the event and replaced it with "OnDetectionFinishedCommand="{Binding BarcodeScannedCommand}"

I then added public ICommand DetectionFinishedCommand { get; set; } to the code behind and in the ScanPage ctor added: (note only changed the ... to {} and closed off with }); Which does not compile) DetectionFinishedCommand = new Command<BarcodeResult[]>(BarcodeResult[] result) => { if (result.Length > 0) {

 }

});

I then changed the above to this which does compile: DetectionFinishedCommand = new Command<BarcodeResult[]>(result => { if (result.Length > 0) {

}

});

The command is never executed and when I break at line 377 in CameraView.cs OnDetectionFinishedCommand is null which make me think the above is still not correct? Yeah I noticed the README was incorrect but I believe the init of the DetectionFinishedCommand in the READMe is incorrect too. I will eventually be using ReactveUI as most of my other projects use that but may also give the Community Toolkit MVVM a try.

NorMoll commented 4 months ago

Just to let you know I changed to CommunityToolkit.Mvvm and the Command Binding now works. Thanks again for the responses.