I am using the fanatisc libary for scanning but I appear to have a new problem. Its crashing out on me after I set my last variable and I do not no why. I think it may have something to do with threads as to the reason why its crashing
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;
}
}
I think it may be the way that the libary calls BeginInvoiteOnMainThread.
I have setup correctly according to documentation with the following in my mainactivy.cs
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);
}
}
}
It crashing right after my onappearing event gets finished. Its all just standard stuff there.
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;
}
}
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an > object.
Thats all i get I don get any further information on the error.
Edit 2
I found that the exception is hapening here in my keyvalue pair and not zxing. SO I adjusted the title it appears to be here.
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;
}
At this line in particualr
KeyValuePair<string, string> selectedEntry= (KeyValuePair<string, string>)pickWarehouse.SelectedItem;