public ViewM(IUIServises uiServices) { this.uiServices = uiServices; drawCommand = new RelayCommand(_ => uiServices.CanDraw(), _ => ModelView.Draw(chart, uiServices.SelectedItems())); deleteCommand = new RelayCommand(_ => { foreach (ModelData model in uiServices.SelectedItems()) { if (!ModelDataOb.Contains(model)) { return(false); } } return(true); }, _ => { foreach (ModelData model in uiServices.SelectedItems()) { ModelDataOb.Remove(model); } } ); saveCommand = new RelayCommand(_ => ModelDataOb.Change, _ => { string path = uiServices.SavePath(); if (path != null) { ModelDataOb.Change = false; ObservableModelData.Save(new SerializeAdapter(File.Create(path)), ModelDataOb); } } ); addCommand = new RelayCommand(_ => uiServices.CanAdd(), _ => ModelDataOb.Add(new ModelData(model.N, model.P)) ); newCommand = new RelayCommand(_ => true, _ => { if (ModelDataOb.Change && uiServices.WantToSave()) { saveCommand.Execute(null); } ModelDataOb.Clear(); ModelDataOb.Change = false; } ); openCommand = new RelayCommand(_ => true, _ => { if (ModelDataOb.Change && uiServices.WantToSave()) { saveCommand.Execute(null); } string path = uiServices.OpenPath(); if (path != null) { ObservableModelData sample = new ObservableModelData(); ObservableModelData.Load(new DeserializeAdapter(File.OpenRead(path)), ref sample); ModelDataOb.copy(sample); } } ); }
public ViewModel(IUIServises uiServices) { this.uiServices = uiServices; ModelParallelizer.OutputEvent += Output; startCommand = new RelayCommand(_ => !running, async _ => { running = true; string[] Files; string Path = uiServices.OpenPath(); try { Files = Directory.GetFiles(Path, "*.*").Where(s => s.EndsWith(".bmp") || s.EndsWith(".jpg") || s.EndsWith(".png") || s.EndsWith(".jpeg")).ToArray(); // 0 images found after LINQ filtering if (Files.Length == 0) { throw new Exception("There is no images in the directory"); } } catch (ArgumentNullException) { running = false; return; } catch (Exception ex) { uiServices.Message(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); running = false; return; } List <ImageRepresentation> Images = new List <ImageRepresentation>(); await Task.Run(() => { float current_count = 0; foreach (string FilePath in Files) { current_count++; byte[] ByteImage = File.ReadAllBytes(FilePath); Images.Add(new ImageRepresentation { ImageName = FilePath, Base64Image = Convert.ToBase64String(ByteImage) }); Progress = (int)(current_count / 100.0); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Progress")); } }); Progress = 0; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Progress")); if (!UseServer) { if (Images != null) { results.Clear(); images.Clear(); await Task.Run(() => ModelParallelizer.Run(Images)); } } else { try { IndeterminedPBar = true; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IndeterminedPBar")); HttpClient client = new HttpClient(); var s = JsonConvert.SerializeObject(Images); var c = new StringContent(s); c.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); cts = new CancellationTokenSource(); HttpResponseMessage result = await client.PutAsync("http://localhost:5000/images", c, cts.Token); string content = await result.Content.ReadAsStringAsync(); List <ImageRepresentation> ResultImages = JsonConvert.DeserializeObject <List <ImageRepresentation> >(content); results.Clear(); images.Clear(); await this.uiServices.GetDispatcher().BeginInvoke(new Action(() => { results.AddResults(ResultImages); })); } catch (HttpRequestException ex) { uiServices.Message("Server is not available: " + ex.Message, "Server Error", MessageBoxButton.OK, MessageBoxImage.Error); } } catch (TaskCanceledException) { }