private async void ImportMapCommandAction() { try { MapAndTrees mapAndTrees = await _fileService.ImportMap(); if (mapAndTrees != null) { BaumCollection = mapAndTrees.baumListe; MapSource = mapAndTrees.map; } } catch (Exception) { _dialogService.ShowErrorDialog("Etwas ist beim importieren schief gelaufen."); } }
public async Task <MapAndTrees> ImportMap() { var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail; picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".svg"); StorageFile file = await picker.PickSingleFileAsync(); MapFile mapFile = new MapFile(); ObservableCollection <Baum> baumCollection = new ObservableCollection <Baum>(); MapAndTrees mapAndTrees = new MapAndTrees(); if (file.FileType == ".svg") { using (IRandomAccessStream stream = await file.OpenReadAsync()) { XNamespace baum_ns = "http://probaumkontrollen.org/probaumkontrollen"; XNamespace svg_ns = "http://www.w3.org/2000/svg"; XNamespace xlink_ns = "http://www.w3.org/1999/xlink"; string svgString = await Windows.Storage.FileIO.ReadTextAsync(file); try { XDocument xDocument = XDocument.Parse(svgString); //Geting the map XElement imageElement = xDocument.Descendants(svg_ns + "image").FirstOrDefault(); string mapString = imageElement.Attribute(xlink_ns + "href").Value; mapString = mapString.Substring(mapString.IndexOf(",") + 1); byte[] mapBytes = Convert.FromBase64String(mapString); using (InMemoryRandomAccessStream imageStream = new InMemoryRandomAccessStream()) { await imageStream.WriteAsync(mapBytes.AsBuffer()); BitmapImage bitmapImage = new BitmapImage(); imageStream.Seek(0); try { await bitmapImage.SetSourceAsync(imageStream); } catch (Exception) { _dialogService.ShowErrorDialog("Die importierte Datei enthält keine gültige Karte."); return(null); } mapFile.MapImageSource = bitmapImage; mapFile.MapBytes = mapBytes; } mapFile.IsSvg = false; mapAndTrees.map = mapFile; // Getting the list of trees XElement xBaumliste = xDocument.Descendants().Where(el => el.HasAttributes && el.FirstAttribute.Name.ToString() == "id").FirstOrDefault(); ObservableCollection <Baum> baumliste = new ObservableCollection <Baum>(); if (!xBaumliste.IsEmpty) { foreach (var node in xBaumliste.Elements().ToList()) { XElement xNode = node; Baum baum = new Baum(); baum.BaumNr = Convert.ToInt16(node.Attribute(baum_ns + "BaumNr").Value); XElement circleNode = node.Elements(svg_ns + "circle").FirstOrDefault(); Windows.Foundation.Point imagePosition; Windows.Foundation.Point canvasPosition; imagePosition.X = double.Parse(circleNode.Attribute("cx").Value, CultureInfo.InvariantCulture); imagePosition.Y = double.Parse(circleNode.Attribute("cy").Value, CultureInfo.InvariantCulture); canvasPosition.X = double.Parse(circleNode.Attribute("cx").Value, CultureInfo.InvariantCulture); canvasPosition.Y = double.Parse(circleNode.Attribute("cy").Value, CultureInfo.InvariantCulture); baum.ImagePosition = imagePosition; baum.CanvasPosition = canvasPosition; baumliste.Add(baum); mapAndTrees.baumListe = baumliste; } } } catch (Exception) { _dialogService.ShowErrorDialog("Die importierte Datei entspricht nicht dem benötigten Format."); return(null); } } } return(mapAndTrees); }