ãXamarin.FormsãQRã³ã¼ãã®èªã¿åããããæ¹æ³
Xamarin.Formsã«ã¯QRã³ã¼ãããã¼ã³ã¼ããèªã¿åãããã®ZXing.Net.Mobileã¨ããã©ã¤ãã©ãªãããã¾ãã
ä»åã¯ãã®ã©ã¤ãã©ãªã使ç¨ãã¦ãXamarin.Formsã§QRã³ã¼ããã¹ãã£ã³ãã¦è¡¨ç¤ºããç°¡åãªã¢ããªãä½ã£ã¦ã¿ã¾ããã
ç°å¢
Visual Studio Community 2017 for Mac
Xamarin.Forms (3.6.0.220655)
ZXing.Net.Mobile (2.4.1)
ZXing.Net.Mobile.Forms (2.4.1)
æé
æºå
ããããã®ããã¸ã§ã¯ãï¼.Net StandardãiOSãAndroidï¼ã«ä»¥ä¸ã®NuGet Packageãã¤ã³ã¹ãã¼ã«ãã¾ãã
- ZXing.Net.Mobile
- ZXing.Net.Mobile.Forms
ãã
ãã
次ã«Androidã®MainActivity.csã¨iOSã®AppDelegate.csã«ã³ã¼ãã®è¿½å ãè¡ãã¾ãã ãã
MainActivity.cs
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { (çç¥) //追å ZXing.Net.Mobile.Forms.Android.Platform.Init(); LoadApplication(new App()); } //追å public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } }
ãã
AppDelegate.csãã
[Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); //追å ZXing.Net.Mobile.Forms.iOS.Platform.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } }
次ã«ç«¯æ«ã§ã«ã¡ã©ã使ç¨ããããã®æºåããã¾ãã
ãã
Androidï¼AndroidManifest.xmlã®ãå¿
è¦ãªã¢ã¯ã»ã¹è¨±å¯ãã®ãã«ã¡ã©ãã«ãã§ãã¯ãå
¥ãã
iOSï¼info.plistãéãã以ä¸ã®é
ç®ã追å ãã
* ããããã£ï¼ãã©ã¤ãã·ã¼-ã«ã¡ã©ã®å©ç¨ç¶æ³ã®èª¬æ
* å¤ï¼"ã«ã¡ã©ãå©ç¨ãã¦ã¹ãã£ã³ãã¾ã"
æå¾ã«ããã²ã¼ã·ã§ã³ãã¼ã¸ã使ãããã«App.xaml.csã«ä»¥ä¸ã®å¤æ´ãè¡ãã¾ãã
public App() { InitializeComponent(); //ãããå¤æ´ MainPage = new NavigationPage(new MainPage()); }
ãã
æºåã¯ããã§OKã§ããããã§ã¯ç»é¢ãä½ã£ã¦ããã¾ãããã
ç»é¢ä½æã¨å¦çã®è¿½å
ããã§ã¯ã¢ããªãä½ã£ã¦ããã¾ãããããã¼ã¸ã¯æ¬¡ã®2ã¤ã ãã§ãã
- MainPageï¼ä¸å¤®ã«ãã¿ã³ããã£ã¦ããããæ¼ãã¨ã¹ãã£ã³ãããã¼ã¸ï¼QRScanPageï¼ã«ç§»å
- QRScanPageï¼èªã¿åããè¡ãã¨ç»é¢ä¸å¤®ã«èªã¿åã£ãå¤ãã¢ã©ã¼ãã«è¡¨ç¤ºããã OKãã¿ã³ãæ¼ãã¨èªã¿åããåé
ãã
æåã«MainPageã§ãããã®ãã¼ã¸ã¯ãã¿ã³ãæ¼ãã¦æ¬¡ã®ãã¼ã¸ã«è¡ãã ãã§ãã
ãã
MainPage.xaml
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:QRCodeStudy" x:Class="QRCodeStudy.MainPage"> <StackLayout> <Button Text="QRã³ã¼ãèªã¿åã" Clicked="OnQR" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> </StackLayout> </ContentPage>
ãã
MainPage.xaml.cs
using System; using Xamarin.Forms; namespace QRCodeStudy { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } void OnQR(object sender, EventArgs e) { Navigation.PushAsync(new QRScanPage()); } } }
次ã«èªã¿åããã¼ã¸ãä½ã£ã¦ããã¾ãã ãã
QRScanPage.xaml
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="QRCodeStudy.QRScanPage" xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"> <ContentPage.Content> <Grid> <zxing:ZXingScannerView x:Name="zxing" OnScanResult="Handle_OnScanResult"/> <zxing:ZXingDefaultOverlay /> </Grid> </ContentPage.Content> </ContentPage>
ZXingScannerViewã®OnScanResultã¤ãã³ãã§èªã¿åã£ãå¤ãã¢ã©ã¼ãã«è¡¨ç¤ºããããã«è¨å®ãã¾ãã ãã
QRScanPage.xaml.cs
using Xamarin.Forms; namespace QRCodeStudy { public partial class QRScanPage : ContentPage { public QRScanPage() { InitializeComponent(); } void Handle_OnScanResult(ZXing.Result result) { Device.BeginInvokeOnMainThread(async () => { zxing.IsAnalyzing = false; //èªã¿åãåæ¢ await DisplayAlert("éç¥","次ã®å¤ãèªã¿åãã¾ããï¼" + result.Text,"OK"); zxing.IsAnalyzing = true; //èªã¿åãåé }); } protected override void OnAppearing() { base.OnAppearing(); zxing.IsScanning = true; } protected override void OnDisappearing() { zxing.IsScanning = false; base.OnDisappearing(); } } }
åãã¯ä»¥ä¸ã®ãããªæãã§ãã
ï¼æå
ã«QRã³ã¼ãããªãã£ãã®ã§ãã¼ã³ã¼ããèªã¿åã£ã¦ãã¾ããQRã³ã¼ããåæ§ã«èªã¿åãäºãã§ãã¾ããï¼
ViewModelã§å¦çãè¡ã
ä¸ã®ã³ã¼ãã¯å¤ãèªã¿åã£ãæã®å¦çãã³ã¼ãããã¤ã³ãã«æ¸ãã¦ãã¾ããããããã¥ã¼ã¢ãã«ã«æ¸ãã¦ã¿ã¾ãããã
QRScanPageãæ¸ãæãã¦ããã¥ã¼ã¢ãã«ã追å ãã¾ããã³ã¼ãã¯ä»¥ä¸ã®éãã§ãã
ãã
QRScanPage.xaml
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="QRCodeStudy.QRScanPage" xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" xmlns:local="clr-namespace:QRCodeStudy"> <ContentPage.BindingContext> <local:QRScanPageViewModel/> </ContentPage.BindingContext> <ContentPage.Content> <AbsoluteLayout> <Grid AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"> <zxing:ZXingScannerView x:Name="zxing" ScanResultCommand="{Binding OnScan}" IsAnalyzing="{Binding IsAnalyzing}"/> <zxing:ZXingDefaultOverlay /> </Grid> <Frame AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.5, 0.5, AutoSize, AutoSize" IsVisible="{Binding FrameVisible}"> <StackLayout> <Label Text="èªã¿åã£ãå¤" HorizontalTextAlignment="Center"/> <Label Text="{Binding ScannedCode}" HorizontalTextAlignment="Center"/> </StackLayout> </Frame> </AbsoluteLayout> </ContentPage.Content> </ContentPage>
ZXingScannerViewã¯ScanResultCommandã¨ããããããã£ãæã£ã¦ãããããªã®ã§ããããã¥ã¼ã¢ãã«ã®Commandã«ãã¤ã³ããã¦ã¿ã¾ããã¾ãããã¥ã¼ã¢ãã«ããDisplayAlertãå¼ã³åºãæ¹æ³ãããããªãã®ã§ãFrameã«ã¹ãã£ã³çµæãè¨è¿°ããããã«ãã¾ãã
ãã
ãã
QRScanPage.xaml.cs
using Xamarin.Forms; namespace QRCodeStudy { public partial class QRScanPage : ContentPage { public QRScanPage() { InitializeComponent(); } protected override void OnAppearing() { base.OnAppearing(); zxing.IsScanning = true; } protected override void OnDisappearing() { zxing.IsScanning = false; base.OnDisappearing(); } } }
ãã
QRScanPageViewModel
using System.ComponentModel; using System.Threading.Tasks; using Xamarin.Forms; namespace QRCodeStudy { public class QRScanPageViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public QRScanPageViewModel() { OnScan = new Command<ZXing.Result>((result)=> { Device.BeginInvokeOnMainThread( async () => { this.IsAnalyzing = false; //èªã¿åãåæ¢ FrameVisible = true; //Frameã表示 ScannedCode = result.Text; await Task.Delay(1000); //1ç§å¾ æ© this.IsAnalyzing = true; //èªã¿åãåé FrameVisible = false; //Frameãé表示 }); }); } public Command OnScan { get; } private bool isAnalyzing; public bool IsAnalyzing { get { return isAnalyzing; } set { if (isAnalyzing != value) { isAnalyzing = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsAnalyzing))); } } } private string scannedCode; public string ScannedCode { get { return scannedCode; } set { if (scannedCode != value) { scannedCode = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ScannedCode))); } } } private bool frameVisible; public bool FrameVisible { get { return frameVisible; } set { if (frameVisible != value) { frameVisible = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FrameVisible))); } } } } }
ã³ã¼ããèªã¿åã£ããèªã¿åã£ãå¤ãè¨è¼ããã¦ããFrameãï¼ç§é表示ãããã®éã¯èªã¿åããããªãããã«ãã¾ããFrameãé表示ã«ãªã£ããèªã¿åããåéããããã«ãã¦ãã¾ãã
ãã
åããè¦ã¦ã¿ã¾ãããã
ãã¾ãåãã¾ããï¼
åèã«ãããã¼ã¸
ZXing.Net Mobile を使ってみた - Android 編 - - Xamarin 日本語情報
ZXing.Net Mobile を使ってみた - iOS、UWP 編 - - Xamarin 日本語情報
Is there a way to render the Zxing ScannerPage in Xaml? — Xamarin Community Forums