コード例 #1
0
        private async void BtnLoadPhoto_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) " +
                         "| *.jpg; *.jpeg; *.jpe; *.jfif; *.png",
            };

            if (dlg.ShowDialog() == true)
            {
                try
                {
                    LogoWindow logo = new LogoWindow(this.Left, this.Top, this.Height, this.Width);
                    logo.Show();

                    await Task.Run(() =>
                    {
                        string filePath = dlg.FileName;
                        var image       = System.Drawing.Image.FromFile(filePath);
                        base64Image     = image.ConvertToBase64String();
                        this.Dispatcher.BeginInvoke((Action)(() => imgPhoto.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(filePath))));
                    });

                    logo.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("There was a problem downloading the file");
                }
            }
        }
コード例 #2
0
        private async void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            tbWarningName.Text  = "";
            tbWarningPrice.Text = "";

            Dictionary <string, string> errorList = new Dictionary <string, string>();

            ProductApiService service = new ProductApiService();

            try
            {
                Decimal price = Convert.ToDecimal(tbPrice.Text);
            }
            catch
            {
                tbWarningPrice.Text = "Неправильне число";
                return;
            }

            if (filtersIdType == -1)
            {
                tbWarningType.Text = "Виберіть тип!";
                return;
            }

            LogoWindow logo = new LogoWindow(this.Left, this.Top, this.Height, this.Width);

            logo.Show();

            errorList = await service.CreateAsync(new ProductAddModel
            {
                Name         = tbName.Text,
                Price        = Convert.ToDecimal(tbPrice.Text),
                PhotoBase64  = base64Image,
                FilterIdType = filtersIdType,
                CategoryId   = categoryIdType
            });

            logo.Close();

            // витягуємо помилки, якщо поля невалідні
            if (errorList != null)
            {
                foreach (var item in errorList)
                {
                    if ("name" == item.Key)
                    {
                        tbWarningName.Text = item.Value;
                    }

                    if ("price" == item.Key)
                    {
                        tbWarningPrice.Text = item.Value;
                    }
                }
            }
            // в іншому випадку - успішно
            else
            {
                this.Close();
            }
        }