2017-08-17 59 views
1

Xamarin開發新手!使用Visual Studio 2017以及我的開發環境的所有最新安裝和更新。Zxing.Net.Mobile Forms和MVVM Xaml未掃描

我有我的應用程序「外殼」工作,它會運行,瀏覽,crud本地數據庫,並同步休息服務。所以,我的應用程序的基礎是健全的。我正在嘗試將ZXing條碼掃描器集成到我的應用程序中。我找到的大部分幫助都與xaml背後的原始表單或代碼有關。我正在使用視圖和視圖模型,我不知道如何將我發現的信息轉換爲此模型。

我目前有一個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> 

視圖模型

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過。

新視圖模型

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

我已經更新了原來的崗位包括在郵寄要求的新的工作視圖模型。

希望有人認爲這篇文章有用。