Zxing.Net.Mobile表单和MVVM Xaml不扫描

时间:2017-08-17 17:16:31

标签: mvvm xamarin.forms zxing.net

Xamarin开发新手!使用Visual Studio 2017以及我的开发环境的所有最新安装和更新。

我的应用程序“shell”正在运行,它将运行,导航,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>();
    }
}

更新 我现在有一个工作视图模型,感谢@Krzysztof在Xamarin.Forms。

新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 个答案:

没有答案