示例#1
0
        /// <summary>
        /// Downloads an image from server and presents it.
        /// </summary>
        /// <param name="id">Images id.</param>
        /// <param name="defaultPath">Default path.</param>
        /// <param name="tempPath">The location of temporary files.</param>
        /// <param name="parent">The panel where an image is supposed to be added.</param>
        /// <param name="type">Data type.</param>
        void AddImage(int id, string defaultPath, string tempPath, Panel parent, ServerData type, bool edit)
        {
            Thread thd = new Thread(new ThreadStart(() =>
            {
                Dispatcher.InvokeAsync(new Action(() => {
                    string img = _proxy.GetItemPropertyAsync(id, type, PropertyData.Imgpath).Result ?? defaultPath;
                    if (img == null)
                    {
                        return;
                    }

                    if (img == defaultPath)
                    {
                        parent.Children.Add(new Image {
                            Source = new BitmapImage(new Uri($"pack://*****:*****@"Temp\{tempPath}"))
                        {
                            Directory.CreateDirectory($@"Temp\{tempPath}");
                        }

                        FilesType filesType = FilesType.Avatars;
                        switch (type)
                        {
                        case ServerData.Video:
                            filesType = FilesType.VideosImages;
                            break;

                        case ServerData.Book:
                            filesType = FilesType.BooksImages;
                            break;

                        case ServerData.User:
                            filesType = FilesType.Avatars;
                            break;

                        case ServerData.Word:
                            filesType = FilesType.WordsImages;
                            break;
                        }
                        byte[] res = _proxy.Download(img, filesType);
                        if (res != null)
                        {
                            try
                            {
                                using (FileStream fs = File.OpenWrite($@"Temp\{tempPath}\{img}"))
                                {
                                    Task.WaitAny(fs.WriteAsync(res, 0, res.Length));
                                    fs.Dispose();
                                }
                            }
                            catch (IOException)
                            {
                                if (edit)
                                {
                                    if (MessageBox.Show("The data have been uploaded to the server. It will be updated the next time you come.\nDo you want to restart now?", "Check next time", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        Process.GetCurrentProcess().Kill();
                                    }
                                }
                            }

                            Image tmp = new Image {
                                Height = 110
                            };
                            FormData.SetImage($@"pack://siteoforigin:,,,/Temp\{tempPath}\{img}", tmp);
                            parent.Children.Insert(0, tmp);
                        }
                    }
                }));
            }))
            {
                IsBackground = true
            };

            thd.Start();
        }