Hi everyone, I want to serialize a MapPolyline on WPF Bing map to a file, close the program, then start the program again and load the file in order to restore that MapPolyline on the map. I couldn't figure how to make this work. Could you please help me to solve this? Thanks in advance.
Here is my code:
private void Save(object sender, RoutedEventArgs e) { System.Windows.Forms.SaveFileDialog saveFile = new System.Windows.Forms.SaveFileDialog(); saveFile.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"; saveFile.FileName = string.Format("{0:MM-dd-yyyy HH.mm.ss}", DateTime.Now); saveFile.DefaultExt = ".xml"; saveFile.InitialDirectory = @"c:\BingMapApp\BingMapState"; if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filename = saveFile.FileName; FileStream Fs = new FileStream(filename, FileMode.Create); foreach (UIElement element in MainMap.Children) { if (element is MapPolyline) { MapPolyline polyline = element as MapPolyline; XamlWriter.Save(polyline, Fs); } } Fs.Close(); } } private void Load(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog(); openFile.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"; if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FileStream xamlFile = openFile.OpenFile() as FileStream; if (xamlFile == null) return; else { MapPolyline element = XamlReader.Load(xamlFile) as MapPolyline; xamlFile.Close(); } }
There's an error with this line: MapPolyline element = XamlReader.Load(xamlFile) as MapPolyline;
It said: "'Failed to create a 'Locations' from the text 'Microsoft.Maps.MapControl.WPF.LocationCollection'."