ClaimedBarcodeScanner.DataReceived 事件

定义

当设备扫描条形码时发生。

// Register
event_token DataReceived(TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs const&> const& handler) const;

// Revoke with event_token
void DataReceived(event_token const* cookie) const;

// Revoke with event_revoker
ClaimedBarcodeScanner::DataReceived_revoker DataReceived(auto_revoke_t, TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs const&> const& handler) const;
public event TypedEventHandler<ClaimedBarcodeScanner,BarcodeScannerDataReceivedEventArgs> DataReceived;
function onDataReceived(eventArgs) { /* Your code */ }
claimedBarcodeScanner.addEventListener("datareceived", onDataReceived);
claimedBarcodeScanner.removeEventListener("datareceived", onDataReceived);
- or -
claimedBarcodeScanner.ondatareceived = onDataReceived;
Public Custom Event DataReceived As TypedEventHandler(Of ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs) 

事件类型

示例

以下示例演示如何设置条形码扫描仪,以在扫描事件后接收数据。

void Scenario1::ScenarioStartScanButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     // Create the barcode scanner. 
    create_task(CreateDefaultScannerObject()).then([this](void)
    {
        if (scanner != nullptr)
        {
            // Claim the scanner for exclusive use by your application.
            create_task(ClaimScanner()).then([this](void)
            {
                if (claimedScanner)
                {
                        
                    // Add a release device requested event handler. If this event is not handled,  
                    // another app can claim the barcode scanner.
                    releaseDeviceRequestedToken = claimedScanner->ReleaseDeviceRequested::add(ref new EventHandler<ClaimedBarcodeScanner^>(this, &Scenario1::OnReleaseDeviceRequested));

  
                    /// Add a data receive event handler.
                    dataReceivedToken =  claimedScanner->DataReceived::add(ref new TypedEventHandler<ClaimedBarcodeScanner^, BarcodeScannerDataReceivedEventArgs^>(this, &Scenario1::OnDataReceived));
                    UpdateOutput("Attached the DataReceived Event handler.");

                    // Set the app to decode the raw data from the barcode scanner 
                    claimedScanner->IsDecodeDataEnabled = true;

                    // Enable the scanner.
                    create_task(EnableScanner()).then([this](void)
                    {
                        UpdateOutput("Ready to Scan.");

                        // Reset the button state
                        ScenarioEndScanButton->IsEnabled = true;
                        ScenarioStartScanButton->IsEnabled = false;
                    });
                }
            });
        }
    });

}
private async void ScenarioStartScanButton_Click(object sender, RoutedEventArgs e)
{
    // Create the barcode scanner. 
    if (await CreateDefaultScannerObject())
    {
        // Claim the scanner for exclusive use by your application.
        if (await ClaimScanner())
        {
            
            // Add a release device requested event handler. If this event is not handled,  
            // another app can claim the barcode scanner.
            claimedScanner.ReleaseDeviceRequested += claimedScanner_ReleaseDeviceRequested;

            // Add a data receive event handler.
            claimedScanner.DataReceived += claimedScanner_DataReceived;
            UpdateOutput("Attached the DataReceived Event handler.");

            // Set the app to decode the raw data from the barcode scanner 
            claimedScanner.IsDecodeDataEnabled = true;

            // Enable the scanner.
            if (await EnableScanner())
            {
                // Reset the button state
                ScenarioEndScanButton.IsEnabled = true;
                ScenarioStartScanButton.IsEnabled = false;

                UpdateOutput("Ready to Scan.");
            }
        } 
    }
    else
    {
        UpdateOutput("No Barcode Scanner found");
    }
}
void Scenario1::OnDataReceived(Windows::Devices::PointOfService::ClaimedBarcodeScanner ^sender, Windows::Devices::PointOfService::BarcodeScannerDataReceivedEventArgs ^args)
{
    // read the data from the buffer and convert to string.
    Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler( 
        [this,args]() 
    {
        DataReader^ scanDataLabelReader = DataReader::FromBuffer(args->Report->ScanDataLabel);
        ScenarioOutputScanDataLabel->Text = scanDataLabelReader->ReadString(args->Report->ScanDataLabel->Length);

        DataReader^  scanDataReader = DataReader::FromBuffer(args->Report->ScanData);
        ScenarioOutputScanData->Text = scanDataReader->ReadString(args->Report->ScanData->Length);

        ScenarioOutputScanDataType->Text = BarcodeSymbologies::GetName(args->Report->ScanDataType);
    }));
}
async void claimedScanner_DataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
{
    // update the UI with the data received from the scan.
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // read the data from the buffer and convert to string.
        var scanDataLabelReader = DataReader.FromBuffer(args.Report.ScanDataLabel);
        ScenarioOutputScanDataLabel.Text = scanDataLabelReader.ReadString(args.Report.ScanDataLabel.Length);

        var scanDataReader = DataReader.FromBuffer(args.Report.ScanData);
        ScenarioOutputScanData.Text = scanDataReader.ReadString(args.Report.ScanData.Length);

        ScenarioOutputScanDataType.Text = BarcodeSymbologies.GetName(args.Report.ScanDataType);

    }); 
}

适用于

另请参阅