如何在WPF Bing地图中序列化MapPolyline? [英] How to serialize MapPolyline in WPF Bing map?

查看:94
本文介绍了如何在WPF Bing地图中序列化MapPolyline?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想将WPF Bing地图上的MapPolyline序列化为文件,关闭程序,然后再次启动程序并加载文件,以便在地图上还原该MapPolyline.我不知道如何使这项工作.请你帮助我好吗 解决这个问题?提前致谢.

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.

这是我的代码:

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();                     

                    }

}

此行存在错误:  MapPolyline元素= XamlReader.Load(xamlFile)为MapPolyline;

There's an error with this line: MapPolyline element = XamlReader.Load(xamlFile) as MapPolyline;

它说:'无法从文本'Microsoft.Maps.MapControl.WPF.LocationCollection'创建'位置'."

It said: "'Failed to create a 'Locations' from the text 'Microsoft.Maps.MapControl.WPF.LocationCollection'."

推荐答案

在保存"中,已将多个MapPolyline保存到文件中,但在加载"中,仅保存了一个. MapPolyline已加载,因此不正确,请尝试以下代码,这可能会有所帮助:

In Save, there are more than one MapPolyline have been saved to the file, but in Load, only one MapPolyline is loaded, so it is not correct, please try below code, it may help:

  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;
                        using (MemoryStream ms = new MemoryStream())
                        {
                            XamlWriter.Save(polyline, ms);
                            byte[] header = BitConverter.GetBytes(ms.Length);
                            Fs.Write(header, 0, header.Length);
                            byte[] data = ms.ToArray();
                            Fs.Write(data, 0, data.Length);
                        }
                    }
                }
                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
                {
                    while (xamlFile.Position < xamlFile.Length - 1)
                    {
                        long len = 0;
                        byte[] header = BitConverter.GetBytes(len);
                        xamlFile.Read(header, 0, header.Length);
                        len = BitConverter.ToInt64(header, 0);

                        byte[] data = new byte[len];
                        xamlFile.Read(data, 0, data.Length);

                        using (MemoryStream ms = new MemoryStream(data))
                        {
                            //get one element
                            MapPolyline element = XamlReader.Load(ms) as MapPolyline; 

                            //add element to map
                            ...
                        }
                    }

                    xamlFile.Close();

                }

            }
        }


这篇关于如何在WPF Bing地图中序列化MapPolyline?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆