Skip to content

Commit

Permalink
Windows 10 Creators Update - March 2017 Update 4
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnewthing committed Mar 30, 2017
2 parents 66db4fc + cebe1f6 commit e8ef3d8
Show file tree
Hide file tree
Showing 97 changed files with 2,460 additions and 812 deletions.
1 change: 1 addition & 0 deletions Samples/BarcodeScanner/cpp/BarcodeScanner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<ClInclude Include="..\..\..\SharedContent\cpp\MainPage.xaml.h">
<DependentUpon>..\..\..\SharedContent\cpp\MainPage.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="..\..\..\SharedContent\cpp\DeviceHelpers.h" />
<ClInclude Include="SampleConfiguration.h" />
<ClInclude Include="Scenario1_BasicFunctionality.xaml.h">
<DependentUpon>..\shared\Scenario1_BasicFunctionality.xaml</DependentUpon>
Expand Down
1 change: 1 addition & 0 deletions Samples/BarcodeScanner/cpp/BarcodeScanner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ClInclude Include="pch.h" />
<ClInclude Include="..\..\..\SharedContent\cpp\App.xaml.h" />
<ClInclude Include="..\..\..\SharedContent\cpp\MainPage.xaml.h" />
<ClInclude Include="..\..\..\SharedContent\cpp\DeviceHelpers.h" />
<ClInclude Include="SampleConfiguration.h" />
<ClInclude Include="Scenario1_BasicFunctionality.xaml.h" />
<ClInclude Include="Scenario2_MultipleScanners.xaml.h" />
Expand Down
11 changes: 11 additions & 0 deletions Samples/BarcodeScanner/cpp/SampleConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
#include "MainPage.xaml.h"
#include "SampleConfiguration.h"

using namespace Concurrency;
using namespace Platform;
using namespace SDKTemplate;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Devices::PointOfService;
using namespace Windows::Foundation;

Platform::Array<Scenario>^ MainPage::scenariosInner = ref new Platform::Array<Scenario>
{
{ "DataReceived Event", "SDKTemplate.Scenario1_BasicFunctionality" },
{ "Release/Retain Functionality", "SDKTemplate.Scenario2_MultipleScanners" },
};

task<BarcodeScanner^> DeviceHelpers::GetFirstBarcodeScannerAsync()
{
return DeviceHelpers::GetFirstDeviceAsync(BarcodeScanner::GetDeviceSelector(),
[](String^ id) { return create_task(BarcodeScanner::FromIdAsync(id)); });
}
6 changes: 6 additions & 0 deletions Samples/BarcodeScanner/cpp/SampleConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include "pch.h"
#include "DeviceHelpers.h"

namespace SDKTemplate
{
Expand Down Expand Up @@ -45,4 +46,9 @@ namespace SDKTemplate
Platform::String^ Title;
Platform::String^ ClassName;
};

namespace DeviceHelpers
{
Concurrency::task<Windows::Devices::PointOfService::BarcodeScanner^> GetFirstBarcodeScannerAsync();
}
}
85 changes: 27 additions & 58 deletions Samples/BarcodeScanner/cpp/Scenario1_BasicFunctionality.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,37 @@ Scenario1_BasicFunctionality::Scenario1_BasicFunctionality() : rootPage(MainPage
InitializeComponent();
}

/// <summary>
/// Creates the default barcode scanner.
/// </summary>
task<void> Scenario1_BasicFunctionality::CreateDefaultScannerObject()
{
return create_task(DeviceInformation::FindAllAsync(BarcodeScanner::GetDeviceSelector())).then([this](DeviceInformationCollection^ deviceCollection)
{
if (deviceCollection == nullptr || deviceCollection->Size == 0)
{
rootPage->NotifyUser("Barcode scanner not found. Please connect a barcode scanner.", NotifyType::ErrorMessage);
return task_from_result();
}

DeviceInformation^ scannerInfo = deviceCollection->GetAt(0);
return create_task(BarcodeScanner::FromIdAsync(scannerInfo->Id)).then([this](BarcodeScanner^ _scanner)
{
this->scanner = _scanner;
if (this->scanner == nullptr)
{
rootPage->NotifyUser("Failed to create barcode scanner object.", NotifyType::ErrorMessage);
}
});
});
}

/// <summary>
/// This method claims the barcode scanner
/// </summary>
task<void> Scenario1_BasicFunctionality::ClaimScanner()
{
// claim the barcode scanner
return create_task(scanner->ClaimScannerAsync()).then([this](ClaimedBarcodeScanner^ _claimedScanner)
{
this->claimedScanner = _claimedScanner;
// enable the claimed barcode scanner
if (claimedScanner == nullptr)
{
rootPage->NotifyUser("Claim barcode scanner failed.", NotifyType::ErrorMessage);
}
});
}

// <summary>
/// Setup the barcode scanner to be ready to receive the data events from the scan.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Scenario1_BasicFunctionality::ScenarioStartScanButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
rootPage->NotifyUser("Creating barcode scanner object.", NotifyType::StatusMessage);
ScenarioStartScanButton->IsEnabled = false;

rootPage->NotifyUser("Acquiring barcode scanner object.", NotifyType::StatusMessage);

// create the barcode scanner.
create_task(CreateDefaultScannerObject()).then([this](void)
// Acquire the barcode scanner.
DeviceHelpers::GetFirstBarcodeScannerAsync().then([this](BarcodeScanner^ foundScanner)
{
scanner = foundScanner;
if (scanner != nullptr)
{
// after successful creation, claim the scanner for exclusive use and enable it so that data reveived events are received.
create_task(ClaimScanner()).then([this](void)
// After acquiring, claim the scanner for exclusive use and enable it so that data received events are received.
create_task(scanner->ClaimScannerAsync()).then([this](ClaimedBarcodeScanner^ _claimedScanner)
{
claimedScanner = _claimedScanner;
if (claimedScanner)
{
// It is always a good idea to have a release device requested event handler. If this event is not handled, there are chances of another app can
// claim ownsership of the barcode scanner.
releaseDeviceRequestedToken = claimedScanner->ReleaseDeviceRequested::add(ref new EventHandler<ClaimedBarcodeScanner^>(this, &Scenario1_BasicFunctionality::OnReleaseDeviceRequested));
releaseDeviceRequestedToken = claimedScanner->ReleaseDeviceRequested += ref new EventHandler<ClaimedBarcodeScanner^>(this, &Scenario1_BasicFunctionality::OnReleaseDeviceRequested);

// after successfully claiming, attach the datareceived event handler.
dataReceivedToken = claimedScanner->DataReceived::add(ref new TypedEventHandler<ClaimedBarcodeScanner^, BarcodeScannerDataReceivedEventArgs^>(this, &Scenario1_BasicFunctionality::OnDataReceived));
// After successfully claiming, attach the datareceived event handler.
dataReceivedToken = claimedScanner->DataReceived += ref new TypedEventHandler<ClaimedBarcodeScanner^, BarcodeScannerDataReceivedEventArgs^>(this, &Scenario1_BasicFunctionality::OnDataReceived);

// Ask the API to decode the data by default. By setting this, API will decode the raw data from the barcode scanner and
// Ask for the raw data from the barcode scanner to be decoded and
// send the ScanDataLabel and ScanDataType in the DataReceived event
claimedScanner->IsDecodeDataEnabled = true;

Expand All @@ -108,15 +70,22 @@ void Scenario1_BasicFunctionality::ScenarioStartScanButton_Click(Platform::Objec
create_task(claimedScanner->EnableAsync()).then([this](void)
{
rootPage->NotifyUser("Ready to scan. Device ID: " + claimedScanner->DeviceId, NotifyType::StatusMessage);

// reset the button state
ScenarioEndScanButton->IsEnabled = true;
ScenarioStartScanButton->IsEnabled = false;
});
}, task_continuation_context::use_current());
}
else
{
rootPage->NotifyUser("Claim barcode scanner failed.", NotifyType::ErrorMessage);
ScenarioStartScanButton->IsEnabled = true;
}
});
}
});
else
{
rootPage->NotifyUser("Barcode scanner not found. Please connect a barcode scanner.", NotifyType::ErrorMessage);
ScenarioStartScanButton->IsEnabled = true;
}
}, task_continuation_context::use_current());

}

Expand Down Expand Up @@ -197,8 +166,8 @@ void Scenario1_BasicFunctionality::ResetTheScenarioState()
if (claimedScanner != nullptr)
{
// Detach the event handlers
claimedScanner->DataReceived::remove(dataReceivedToken);
claimedScanner->ReleaseDeviceRequested::remove(releaseDeviceRequestedToken);
claimedScanner->DataReceived -= dataReceivedToken;
claimedScanner->ReleaseDeviceRequested -= releaseDeviceRequestedToken;

// release the Barcode Scanner and set to null
delete claimedScanner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ namespace SDKTemplate
void ScenarioStartScanButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void ScenarioEndScanButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

Concurrency::task<void> CreateDefaultScannerObject();
Concurrency::task<void> ClaimScanner();
void UpdateOutput(Platform::String^ strMessage);
void ResetTheScenarioState();

void OnDataReceived(Windows::Devices::PointOfService::ClaimedBarcodeScanner ^sender, Windows::Devices::PointOfService::BarcodeScannerDataReceivedEventArgs ^args);
Expand Down
3 changes: 3 additions & 0 deletions Samples/BarcodeScanner/cs/BarcodeScanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<Compile Include="..\..\..\SharedContent\cs\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\..\..\SharedContent\cs\DeviceHelpers.cs">
<Link>DeviceHelpers.cs</Link>
</Compile>
<Compile Include="SampleConfiguration.cs" />
<Compile Include="Scenario1_BasicFunctionality.xaml.cs">
<DependentUpon>Scenario1_BasicFunctionality.xaml</DependentUpon>
Expand Down
11 changes: 11 additions & 0 deletions Samples/BarcodeScanner/cs/SampleConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;
using Windows.UI.Xaml.Controls;

namespace SDKTemplate
Expand All @@ -31,4 +34,12 @@ public class Scenario
public string Title { get; set; }
public Type ClassType { get; set; }
}

public partial class DeviceHelpers
{
public static async Task<BarcodeScanner> GetFirstBarcodeScannerAsync()
{
return await DeviceHelpers.GetFirstDeviceAsync(BarcodeScanner.GetDeviceSelector(), async (id) => await BarcodeScanner.FromIdAsync(id));
}
}
}
80 changes: 20 additions & 60 deletions Samples/BarcodeScanner/cs/Scenario1_BasicFunctionality.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,6 @@ public Scenario1_BasicFunctionality()
this.InitializeComponent();
}

/// <summary>
/// Creates the default barcode scanner.
/// </summary>
/// <returns>true if barcode scanner is created. Otherwise returns false</returns>
private async Task<bool> CreateDefaultScannerObject()
{
if (scanner == null)
{
rootPage.NotifyUser("Creating barcode scanner object.", NotifyType.StatusMessage);
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(BarcodeScanner.GetDeviceSelector());
if (deviceCollection != null && deviceCollection.Count > 0)
{
scanner = await BarcodeScanner.FromIdAsync(deviceCollection[0].Id);

if (scanner == null)
{
rootPage.NotifyUser("Failed to create barcode scanner object.", NotifyType.ErrorMessage);
return false;
}
}
else
{
rootPage.NotifyUser("Barcode scanner not found. Please connect a barcode scanner.", NotifyType.ErrorMessage);
return false;
}
}

return true;
}

/// <summary>
/// This method claims the barcode scanner
/// </summary>
/// <returns>true if claim is successful. Otherwise returns false</returns>
private async Task<bool> ClaimScanner()
{
if (claimedScanner == null)
{
// claim the barcode scanner
claimedScanner = await scanner.ClaimScannerAsync();

// enable the claimed barcode scanner
if (claimedScanner == null)
{
rootPage.NotifyUser("Claim barcode scanner failed.", NotifyType.ErrorMessage);
return false;
}
}
return true;
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
Expand Down Expand Up @@ -121,15 +70,19 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
/// <param name="e"></param>
private async void ScenarioStartScanButton_Click(object sender, RoutedEventArgs e)
{
rootPage.NotifyUser("Creating barcode scanner object.", NotifyType.StatusMessage);
ScenarioStartScanButton.IsEnabled = false;

// create the barcode scanner.
if (await CreateDefaultScannerObject())
rootPage.NotifyUser("Acquiring barcode scanner object.", NotifyType.StatusMessage);

// Acquire the barcode scanner.
scanner = await DeviceHelpers.GetFirstBarcodeScannerAsync();
if (scanner != null)
{
// after successful creation, claim the scanner for exclusive use and enable it so that data reveived events are received.
if (await ClaimScanner())
{
claimedScanner = await scanner.ClaimScannerAsync();

if (claimedScanner != null)
{
// It is always a good idea to have a release device requested event handler. If this event is not handled, there are chances of another app can
// claim ownsership of the barcode scanner.
claimedScanner.ReleaseDeviceRequested += claimedScanner_ReleaseDeviceRequested;
Expand All @@ -146,13 +99,20 @@ private async void ScenarioStartScanButton_Click(object sender, RoutedEventArgs
// if the claimedScanner has not beed Enabled
await claimedScanner.EnableAsync();

// reset the button state
ScenarioEndScanButton.IsEnabled = true;
ScenarioStartScanButton.IsEnabled = false;

rootPage.NotifyUser("Ready to scan. Device ID: " + claimedScanner.DeviceId, NotifyType.StatusMessage);
ScenarioEndScanButton.IsEnabled = true;
}
else
{
rootPage.NotifyUser("Claim barcode scanner failed.", NotifyType.ErrorMessage);
ScenarioStartScanButton.IsEnabled = true;
}
}
else
{
rootPage.NotifyUser("Barcode scanner not found. Please connect a barcode scanner.", NotifyType.ErrorMessage);
ScenarioStartScanButton.IsEnabled = true;
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Samples/BarcodeScanner/js/BarcodeScanner.jsproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<Content Include="..\..\..\SharedContent\js\js\default.js">
<Link>js\default.js</Link>
</Content>
<Content Include="..\..\..\SharedContent\js\js\devicehelpers.js">
<Link>js\devicehelpers.js</Link>
</Content>
<Content Include="..\..\..\SharedContent\js\css\default.css">
<Link>css\default.css</Link>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<head>
<title></title>
<link rel="stylesheet" href="/css/scenario1_BasicFunctionality.css">
<script src="/js/devicehelpers.js"></script>
<script src="/js/scenario1_BasicFunctionality.js"></script>
<script src="/js/util.js"></script>
</head>
Expand Down
9 changes: 8 additions & 1 deletion Samples/BarcodeScanner/js/js/sample-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
{ url: "/html/scenario2_MultipleScanners.html", title: "Release/Retain functionality" }
];

var BarcodeScanner = Windows.Devices.PointOfService.BarcodeScanner;

function getFirstBarcodeScannerAsync() {
return DeviceHelpers.getFirstDeviceAsync(BarcodeScanner.getDeviceSelector(), (id) => BarcodeScanner.fromIdAsync(id));
}

WinJS.Namespace.define("SdkSample", {
sampleTitle: sampleTitle,
scenarios: new WinJS.Binding.List(scenarios)
scenarios: new WinJS.Binding.List(scenarios),
getFirstBarcodeScannerAsync: getFirstBarcodeScannerAsync
});
})();
Loading

0 comments on commit e8ef3d8

Please sign in to comment.