2017-08-17 4 views
1

Xamarin開発の新機能! Visual Studio 2017とすべての最新のインストールと更新を使用して私の開発環境。Zxing.Net.MobileフォームとMVVM Xamlがスキャンされない

私は自分のアプリ「シェル」を動かし、ナビゲートし、ローカルDBにクラッシュさせ、サービスを休止するという点で機能します。だから、私のアプリのベースは健全です。私は私のアプリにZXingバーコードスキャナを統合しようとしています。私が見つけることができるもののほとんどは、生のフォームやxamlの背後にあるコードに関連しています。私はビューとビューモデルを使用していますが、私がこのモデルに見いだしている情報をどのように翻訳するのか分かりません。

私は現在、ボタンをクリックすると "カメラビューア"を表示しているので、カメラを使ってバーコードを見ることができます。しかし、カメラビューには「赤い線」はなく、その後、何が起こっているかを知らせるためのイベントは発生しません。私はXamarinにとってとても新しいので、私は本当に混乱しています。私はどこから始めるべきかわかりません。

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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
     xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials" 
     xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" 
     prism:ViewModelLocator.AutowireViewModel="True" 
     x:Class="Views.InventoryPage1" 
    Title="Inventory Page"> 

<StackLayout Spacing="5" Padding="10,10,10,0"> 
    <!--<fe:BindablePicker ItemsSource="{Binding Areas}" SelectedItem="{Binding SelectedArea}" DisplayProperty="AreaName" Title="Select Your Area" /> 
    <fe:BindablePicker ItemsSource="{Binding Reasons}" SelectedItem="{Binding SelectedReason}" 
     DisplayProperty="Description" Title="Select a Reason" /> 
    <Label Text="Scan"/> 
    <Entry Text="{Binding Barcode}"/> 
    <Label Text="Quantity"/> 
    <Entry Text="{Binding Quantity}"/> 
    <Button Text="Save" Style="{StaticResource Button_Primary}" Command="{Binding SaveCommand}" />--> 

    <forms:ZXingScannerView WidthRequest="100" HeightRequest="100" IsScanning="{Binding IsScanning}" IsAnalyzing="{Binding IsAnalyzing}" Result="{Binding Result, Mode=TwoWay}" ScanResultCommand="{Binding QRScanResultCommand}" ></forms:ZXingScannerView> 
    </StackLayout> 
</ContentPage> 

のViewModel

public class InventoryPage1ViewModel : BindableBase, INavigationAware 
{ 
    private readonly IPageDialogService _pageDialogService; 
    private bool _isAnalyzing = true; 
    private bool _isScanning = true; 
    public ZXing.Result Result { get; set; } 

    public List<Area> Areas { get; private set; } 
    public Area SelectedArea { get; set; } 

    public List<Reason> Reasons { get; private set; } 
    public Reason SelectedReason { get; set; } 

    public int Quantity { get; set; } 
    public string Barcode { get; set; } 

    public DelegateCommand SaveCommand => new DelegateCommand(PerformSave); 
    public DelegateCommand QRScanResultCommand => new DelegateCommand(QRCommand); 

    private readonly IAreaService _areaService; 
    private readonly IScanService _scanService; 
    private readonly IReasonService _reasonService; 

    public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService) 
    { 
     _pageDialogService = pageDialogService; 
     _reasonService = reasonService; 
     _scanService = scanService; 
     _areaService = areaService; 
     Areas = _areaService.GetAll(); 
     Reasons = _reasonService.GetAll(); 
    } 

    public bool IsScanning 
    { 
     get 
     { 
      return _isScanning; 
     } 
     set 
     { 
      _isScanning = value; 
      RaisePropertyChanged(); 
     } 
    } 

    public bool IsAnalyzing 
    { 
     get 
     { 
      return _isAnalyzing; 
     } 
     set 
     { 
      _isAnalyzing = value; 
      RaisePropertyChanged(); 
     } 
    } 

    private void QRCommand() 
    { 
     int x = 1; 
    } 

    private async void PerformSave() 
    { 
     var scan = new Scan() 
     { 
      AreaId = SelectedArea.Id, 
      InsertDateTime = DateTime.Now, 
      ReasonId = SelectedReason.Id, 
      ScanItem = Barcode, 
      ScanQty = Quantity, 
      IsUploaded = false 
     }; 

     // Save it to the DB here. 
     var retVal = _scanService.Insert(scan); 

     if (retVal) 
     { 
      await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK"); 
     } 
     else 
     { 
      // TODO: Inform the user something went wrong. 
     } 
    } 

    int _index; 
    public int SelectIndex 
    { 
     get 
     { 
      return _index; 
     } 
     set 
     { 
      _index = value; 
      RaisePropertyChanged("SelectIndex"); 
     } 
    } 

    public void OnNavigatedFrom(NavigationParameters parameters) 
    { 
    } 

    public void OnNavigatedTo(NavigationParameters parameters) 
    { 
    } 

    public void OnNavigatingTo(NavigationParameters parameters) 
    { 
    } 
} 

MainActivity

public class MainActivity : 
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     TabLayoutResource = Resource.Layout.tabs; 
     ToolbarResource = Resource.Layout.toolbar; 

     base.OnCreate(bundle); 

     global::Xamarin.Forms.Forms.Init(this, bundle); 
     ZXing.Net.Mobile.Forms.Android.Platform.Init(); 

     LoadApplication(new App(new AndroidInitializer())); 

    } 

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) 
    { 
     ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 

public class AndroidInitializer : IPlatformInitializer 
{ 
    public void RegisterTypes(IUnityContainer container) 
    { 
     container.RegisterType<IConnectionFactory, ConnectionFactory>(); 
    } 
} 

UPDATE 私は今Xamarin.Formsでオーバー@Krzysztofのおかげで作業ビューモデルを持っています。

新しいViewModelに

public class InventoryPage1ViewModel : BindableBase, INavigationAware 
{ 
    private readonly IPageDialogService _pageDialogService; 
    public List<Area> Areas { get; private set; } 
    public Area SelectedArea { get; set; } 

    public List<Reason> Reasons { get; private set; } 
    public Reason SelectedReason { get; set; } 

    public int Quantity { get; set; } 

    public DelegateCommand SaveCommand => new DelegateCommand(PerformSave); 

    private readonly IAreaService _areaService; 
    private readonly IScanService _scanService; 
    private readonly IReasonService _reasonService; 

    public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService) 
    { 
     _pageDialogService = pageDialogService; 
     _reasonService = reasonService; 
     _scanService = scanService; 
     _areaService = areaService; 
     Areas = _areaService.GetAll(); 
     Reasons = _reasonService.GetAll(); 
    } 

    public ZXing.Result Result { get; set; } 

    private string barcode = string.Empty; 
    public string Barcode 
    { 
     get 
     { 
      return barcode; 
     } 
     set 
     { 
      barcode = value; 
      RaisePropertyChanged(); 
     } 
    } 

    private bool isAnalyzing = true; 
    public bool IsAnalyzing 
    { 
     get { return this.isAnalyzing; } 
     set 
     { 
      if (!bool.Equals(this.isAnalyzing, value)) 
      { 
       this.isAnalyzing = value; 
       RaisePropertyChanged(nameof(IsAnalyzing)); 
      } 
     } 
    } 

    private bool isScanning = true; 
    public bool IsScanning 
    { 
     get { return this.isScanning; } 
     set 
     { 
      if (!bool.Equals(this.isScanning, value)) 
      { 
       this.isScanning = value; 
       RaisePropertyChanged(nameof(IsScanning)); 
      } 
     } 
    } 

    public Command QRScanResultCommand 
    { 
     get 
     { 
      return new Command(() => 
      { 
       IsAnalyzing = false; 
       IsScanning = false; 

       Device.BeginInvokeOnMainThread(async() => 
       { 
        Barcode = Result.Text; 
        await _pageDialogService.DisplayAlertAsync("Scanned Item", Result.Text, "Ok"); 
       }); 

       IsAnalyzing = true; 
       IsScanning = true; 
      }); 
     } 
    } 
    private async void PerformSave() 
    { 
     var scan = new Scan() 
     { 
      AreaId = SelectedArea.Id, 
      InsertDateTime = DateTime.Now, 
      ReasonId = SelectedReason.Id, 
      ScanItem = Barcode, 
      ScanQty = Quantity, 
      IsUploaded = false 
     }; 

     // Save it to the DB here. 
     var retVal = _scanService.Insert(scan); 

     if (retVal) 
     { 
      await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK"); 
     } 
     else 
     { 
      // TODO: Inform the user something went wrong. 
     } 
    } 

    public void OnNavigatedFrom(NavigationParameters parameters) 
    { 
    } 

    public void OnNavigatedTo(NavigationParameters parameters) 
    { 
    } 

    public void OnNavigatingTo(NavigationParameters parameters) 
    { 
    } 
} 

答えて

0

私はポストにのために頼まれた新しい作業のViewModelを含めるために、オリジナルのポストを更新しました。

この投稿は役に立ちましたか?

関連する問題