コード例 #1
0
        private async void BtnAddItem_Click(object sender, RoutedEventArgs e)
        {
            MessageDialog md;

            if (cvm.Categories.Count != 0)
            {
                var name         = Naam.Text;
                var amount       = 1;
                var categoryName = "";
                try
                {
                    amount = Convert.ToInt32(Amount.Text);
                }
                catch (FormatException)
                {
                    md = new MessageDialog("Enter a valid number");
                    await md.ShowAsync();
                }

                if (cmbCategory.SelectedItem != null)
                {
                    categoryName = cmbCategory.SelectedItem.ToString();
                }
                else
                {
                    md = new MessageDialog("You have to select a category!");
                }

                if (Naam.Text.Length != 0)
                {
                    if (Amount.Text.Length != 0)
                    {
                        if (amount > 0)
                        {
                            if (cmbCategory.SelectedItem != null)
                            {
                                await cvm.AddNewItem(name, amount, categoryName);

                                Naam.Text         = "";
                                Amount.Text       = "";
                                LvCat.ItemsSource = null;
                                LvCat.ItemsSource = cvm.Categories;
                                progressbar.Maximum++;
                                var percentageDone = progressbar.Value / progressbar.Maximum * 100;
                                ItemsDone.Text = Math.Round(percentageDone).ToString() + "%";
                            }
                            else
                            {
                                md = new MessageDialog("Please select a category.");
                                await md.ShowAsync();
                            }
                        }
                        else
                        {
                            md = new MessageDialog("Amount can't be less than or equal to 0!");
                            await md.ShowAsync();
                        }
                    }
                    else
                    {
                        md = new MessageDialog("Amount can't be empty!");
                        await md.ShowAsync();
                    }
                }
                else
                {
                    md = new MessageDialog("Name can't be empty!");
                    await md.ShowAsync();
                }
            }
            else
            {
                md = new MessageDialog("You can't add an item without a category!");
                await md.ShowAsync();
            }
        }