使用扫描仪页面后崩溃

时间:2019-05-27 20:35:49

标签: c# xamarin.forms zxing

我正在使用fanatisc库进行扫描,但似乎有一个新问题。设置完最后一个变量后,它突然崩溃了,我也不是为什么。我认为这可能与线程有关,导致其崩溃

conda

我认为这可能是库调用BeginInvoiteOnMainThread的方式。

我已经根据文档正确设置了mainactivy.cs中的以下内容

private async void BtnTestScan_Clicked(object sender, EventArgs e)
{

        var scanPage = new ZXingScannerPage();
        scanPage.ToggleTorch();
        scanPage.IsScanning = true;
        // Navigate to our scanner page
        await Navigation.PushAsync(scanPage);


        scanPage.OnScanResult += (result) =>
        {


            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread(async () =>
            {
                BomComponentData getInfo = new BomComponentData();


                await Navigation.PopAsync();
                if (isBomScan.IsToggled == false)
                {
                    getInfo = await database.GetProductInfoByBarCode(result.Text);


                    lblOpertationName.Text = "Operation Name: " + getInfo.OperationName;
                    //var popup1 = new FuelStockApp.Views.PopupInputBox("You scanned the item " + getInfo.Name + "You need to pick " + getInfo.Quantity.ToString(), Convert.ToInt16(getInfo.Quantity));
                    BarCode = result.Text;
                    //await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(popup1);
                    Int32.TryParse(txtQtyUpdate.Text, out int Qty);
                    lblQty.Text = Qty.ToString();
                    gridItems.IsVisible = false;
                    txtCode.Text = BomId;
                    lblOpertationName.Text = "Operation Name: " + getInfo.OperationName;
                    lblBinLocation.Text = "Bin Location : " + getInfo.BinLocation.ToString();
                    lblBinLocationLabel.Text = "Bin Name : " + getInfo.BinName.ToString();

                    QtyToDeduct = Qty;
                }

}

我即将发生的事件结束后立即崩溃。那里只是标准的东西。

public class MainActivity : 

global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            Rg.Plugins.Popup.Popup.Init(this,savedInstanceState);

            LoadApplication(new App());
            }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
  

未处理的异常:

     

System.NullReferenceException:对象引用未设置为>对象的实例。

这就是我所得到的,我没有得到关于该错误的任何进一步信息。

编辑2

我发现异常在我的键值对中发生,而不是zxing。所以我调整了标题,似乎在这里。

protected async override void OnAppearing()
{
         base.OnAppearing();


        await database.copydb();

        List<BomComponentData> _boms = new List<BomComponentData>();


        _boms = await database.GetBomsAsync(BomId);
        viewModel.Boms = _boms.ToObservableCollection();
        viewModel.LoadItemsCommand.Execute(null);
        warehouses.RemoveAll (x => x.WarehouseID > 0);
        PickerItems.Clear();
        PickerItemList.Clear();
        warehouses = await database.GetWarehousesFromSage();
        if (warehouses.Count > 0)
        {
            viewModel.Warehouses = warehouses.ToObservableCollection();
            foreach (var warehouse in warehouses)
            {
                if (warehouse.Description == "")
                    warehouse.Description = warehouse.Name;


                PickerItems.Add(warehouse.WarehouseID.ToString(), warehouse.Description);

            }
            pickWarehouse.ItemsSource = PickerItemList;
        }

}

特别是在这一行

  private async void PickWarehouse_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnTestScan.IsVisible = true;
        gridItems.IsVisible = true;
        txtCode.IsVisible = true;


        KeyValuePair<string, string> selectedEntry= (KeyValuePair<string, string>)pickWarehouse.SelectedItem;

        List<BomComponentData> _boms = new List<BomComponentData>();


        WarehouseId = long.Parse(selectedEntry.Key);
        _boms = await database.GetBomsAsync(txtCode.Text);
        viewModel.Boms = _boms.ToObservableCollection();
        gridItems.ItemsSource = _boms;


    }

0 个答案:

没有答案