App crashes the first time I select an item in a combobox defined programmatically in a grid that is added as a child of a stack panel

0

I get the following error on a Zebra tablet t55 when running my app:

Unhandled exception at 0x00007FFEA4A22D45 (Windows.UI.Xaml.dll) in PerfectDelivery.exe: 0xC000041D: An unhandled exception was encountered during a user callback.

It happens after I load all my sqlite database and run the app the first time. After the crash I start the app back up again and it runs fine. The offending Combobox is here:

     private void InitProducts()
    {
        StpProducts.Children.Clear();
        _lstItemsInMultipleCrates = new List<int>();
        Invoice deliveryDetails = new Invoice(((App)Application.Current).OrderId);
        ItemsInCrate = new List<InvoiceItem>();
        if (_selectedCrate != null)
        {
            btnFinishCrate.Visibility = Visibility.Visible;
            foreach (InvoiceItem i in deliveryDetails.InvoiceItems.Where(
                x => x.CrateNumber.CrateId == _selectedCrate))
            {
                ItemsInCrate.Add(i);

                GridProducts = new Grid();

                GridProducts.Height = 35;
                GridProducts.Width = 600;
                GridProducts.HorizontalAlignment = HorizontalAlignment.Left;

                ColumnDefinition cdProductName = new ColumnDefinition();
                cdProductName.Width = new GridLength(170, GridUnitType.Star);
                ColumnDefinition cdQty = new ColumnDefinition();
                cdQty.Width = new GridLength(45, GridUnitType.Star);
                ColumnDefinition cdUnits = new ColumnDefinition();
                cdUnits.Width = new GridLength(60, GridUnitType.Star);
                ColumnDefinition cdChecked = new ColumnDefinition();
                cdChecked.Width = new GridLength(30, GridUnitType.Star);
                ColumnDefinition cdDropdown = new ColumnDefinition();
                cdDropdown.Width = new GridLength(80, GridUnitType.Star);
                ColumnDefinition cdFiller = new ColumnDefinition();
                cdFiller.Width = new GridLength(1, GridUnitType.Star);

                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(35, GridUnitType.Star);

                GridProducts.ColumnDefinitions.Add(cdProductName);
                GridProducts.ColumnDefinitions.Add(cdQty);
                GridProducts.ColumnDefinitions.Add(cdUnits);
                GridProducts.ColumnDefinitions.Add(cdChecked);
                GridProducts.ColumnDefinitions.Add(cdDropdown);



                TextBlock txbOrderDetailPickedId = new TextBlock { Name = "ODPID" };
                txbOrderDetailPickedId.Text = i.OrderDetailPickedId.ToString("G");
                txbOrderDetailPickedId.Visibility = Visibility.Collapsed;

                TextBlock txbOriginalQty = new TextBlock { Name = "OQ" };
                txbOriginalQty.Text = i.Quantity.ToString("N");
                txbOriginalQty.Visibility = Visibility.Collapsed;


                List<long?> lstCrateCount = new List<long?>();
                foreach (InvoiceItem crateCount in deliveryDetails.InvoiceItems.Where(x => x.CrateNumber.OrderDetailPickedId == i.OrderDetailPickedId))
                {

                    lstCrateCount.Add(crateCount.CrateNumber.CrateId);


                }// return count of crates for item. If > 1 display crate number(s)
                TextBlock txbProductName = new TextBlock();


                TextBox txbQuantity = new TextBox();
                txbQuantity.Text = i.Quantity.ToString("N");
                txbQuantity.Height = 35;

                InputScope scope = new InputScope();
                InputScopeName scopeName = new InputScopeName { NameValue = InputScopeNameValue.Number };
                scope.Names.Add(scopeName);
                txbQuantity.InputScope = scope;

                TextBlock txbUnits = new TextBlock();
                txbUnits.Text = i.PackDescription;
                txbUnits.HorizontalTextAlignment = TextAlignment.Center;
                txbUnits.VerticalAlignment = VerticalAlignment.Center;

                CheckBox chkChecked = new CheckBox();

            ---> CmbDiscrepancies = new ComboBox{ SelectedIndex = -1};

                if (CmbDiscrepancies?.Items != null)
                {
                    CmbDiscrepancies.Items.Add("Correct");
                    CmbDiscrepancies.Items.Add("Damaged");
                    CmbDiscrepancies.Items.Add("Missing");
                    CmbDiscrepancies.Items.Add("Wrong Item");
                    CmbDiscrepancies.Items.Add("Out of Stock");
                    CmbDiscrepancies.Items.Add("Incorrect Amt");
                }

                CmbDiscrepancies.SelectedIndex = 0;
                CmbDiscrepancies.VerticalAlignment = VerticalAlignment.Center;*

                if (lstCrateCount.Count == 1)
                {
                    txbProductName.Text = i.ProductName;

                }
                else if (!Crate.ItemInMultipleCrates(i.OrderDetailPickedId))
                {

                    _lstItemsInMultipleCrates.Add(i.OrderDetailPickedId);

                    txbProductName.TextWrapping = TextWrapping.Wrap;
                    txbProductName.Text = i.ProductName + "\r\n" + "(";
                    int count = 0;
                    foreach (long? crate in lstCrateCount)
                    {
                        count++;
                        txbProductName.Text += count != lstCrateCount.Count ? crate + ", " : crate + ")\r\nQty is the total in all crates listed above.";
                    }

                    GridProducts.Height = 50 * (lstCrateCount.Count * .66);

                    row.Height = new GridLength(GridProducts.Height, GridUnitType.Pixel);

                }

                if (Crate.ItemInMultipleCrates(i.OrderDetailPickedId))
                {
                    txbProductName.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
                    CmbDiscrepancies.IsEnabled = false;
                    txbQuantity.IsEnabled = false;
                    if (_lstItemsInMultipleCrates.Count > 0)
                        GridProducts.Height = 35 * (_lstItemsInMultipleCrates.Count * 1.32);
                    else
                    {
                        GridProducts.Height = 35 * 1.32;
                    }
                    row.Height = new GridLength(GridProducts.Height, GridUnitType.Pixel);
                    txbProductName.Text = i.ProductName + "\r\n" + "(Item is in multiple crates)";
                    chkChecked.IsChecked = true;
                    chkChecked.IsEnabled = false;

                }

                GridProducts.Children.Add(txbOriginalQty);
                GridProducts.Children.Add(txbOrderDetailPickedId);
                GridProducts.Children.Add(txbProductName);
                GridProducts.Children.Add(txbQuantity);
                GridProducts.Children.Add(txbUnits);
                GridProducts.Children.Add(chkChecked);
                GridProducts.Children.Add(CmbDiscrepancies);

                Grid.SetColumn(txbProductName, 0);
                Grid.SetColumn(txbQuantity, 1);
                Grid.SetColumn(txbUnits, 2);
                Grid.SetColumn(chkChecked, 3);
                Grid.SetColumn(CmbDiscrepancies, 4);

                Grid.SetRow(txbProductName, 0);
                Grid.SetRow(txbQuantity, 0);
                Grid.SetRow(txbUnits, 0);
                Grid.SetRow(chkChecked, 0);
                Grid.SetRow(CmbDiscrepancies, 0);
                GridProducts.RowDefinitions.Add(row);


                StpProducts.Children.Add(GridProducts);

            }


        }

The XAML is

         <ScrollViewer x:Name="ScvProducts" HorizontalAlignment="Left" Height="354" Margin="375,146,0,0" VerticalAlignment="Top" Width="638" BorderBrush="#FF1B933C" BorderThickness="2" >
        <StackPanel x:Name="StpProducts" MinHeight="450" Width="612" HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Vertical" Height="995" >

                <Grid x:Name="GridProducts">
                    <ComboBox x:Name="CmbDiscrepancies" Visibility></ComboBox>
                </Grid>

        </StackPanel>
    </ScrollViewer>
combobox
uwp
stackpanel
asked on Stack Overflow Feb 20, 2019 by stevenbam

1 Answer

0

Upgrading from Windows 10 1709 to 1803 fixed the existing code. I also got rid of the stack panel and add the row definitions dynamically which seems to have addressed the 1709 version.

answered on Stack Overflow Feb 22, 2019 by stevenbam

User contributions licensed under CC BY-SA 3.0