Created
August 20, 2018 05:10
-
-
Save codercampos/1c38742d627daf0ce775dd685e1a123e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ContentPage Title="Barcode Demo" | |
xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:BarcodeScanner" | |
xmlns:viewmodels="clr-namespace:BarcodeScanner.ViewModels;assembly=BarcodeScanner" | |
x:Class="BarcodeScanner.Views.MainPage"> | |
<ContentPage.BindingContext> | |
<viewmodels:MainPageViewModel /> | |
</ContentPage.BindingContext> | |
<StackLayout Padding="20"> | |
<!-- Place new controls here --> | |
<Button Command="{Binding ButtonCommand}" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" BackgroundColor="Teal" TextColor="White" FontSize="Medium" Text="Click to open scanner" /> | |
<Label Text="{Binding Result}" HorizontalOptions="Center" VerticalOptions="StartAndExpand" /> | |
</StackLayout> | |
</ContentPage> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
using System.Windows.Input; | |
using Xamarin.Forms; | |
using ZXing; | |
using ZXing.Mobile; | |
using ZXing.Net.Mobile.Forms; | |
namespace BarcodeScanner.ViewModels | |
{ | |
public class MainPageViewModel : INotifyPropertyChanged | |
{ | |
private string _result; | |
public string Result | |
{ | |
get => _result; | |
set | |
{ | |
_result = value; | |
OnPropertyChanged(nameof(Result)); | |
} | |
} | |
public ICommand ButtonCommand { get; private set; } | |
public MainPageViewModel() | |
{ | |
ButtonCommand = new Command(OnButtomCommand); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void OnPropertyChanged([CallerMemberName] string propertyName = "") | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
private void OnButtomCommand() | |
{ | |
var options = new MobileBarcodeScanningOptions(); | |
options.PossibleFormats = new List<BarcodeFormat> | |
{ | |
BarcodeFormat.QR_CODE, | |
BarcodeFormat.CODE_128, | |
BarcodeFormat.EAN_13 | |
}; | |
var page = new ZXingScannerPage(options) { Title = "Scanner" }; | |
var closeItem = new ToolbarItem { Text = "Close" }; | |
closeItem.Clicked += (object sender, EventArgs e) => | |
{ | |
page.IsScanning = false; | |
Device.BeginInvokeOnMainThread(() => | |
{ | |
Application.Current.MainPage.Navigation.PopModalAsync(); | |
}); | |
}; | |
page.ToolbarItems.Add(closeItem); | |
page.OnScanResult += (result) => | |
{ | |
page.IsScanning = false; | |
Device.BeginInvokeOnMainThread(() => { | |
Application.Current.MainPage.Navigation.PopModalAsync(); | |
if (string.IsNullOrEmpty(result.Text)) | |
{ | |
Result = "No valid code has been scanned"; | |
} | |
else | |
{ | |
Result = $"Result: {result.Text}"; | |
} | |
}); | |
}; | |
Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(page) { BarTextColor = Color.White, BarBackgroundColor = Color.CadetBlue }, true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment