예제 #1
0
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void AddLayerDataBtn_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter           = "Shapefiles|*.shp|Image files|*.bmp;*.png;*.sid;*.tif";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.Multiselect      = false;
                openFileDialog.Title            = "文件选取";
                if (openFileDialog.ShowDialog() == true)
                {
                    string item = openFileDialog.SafeFileNames[0].Split('.')[1];
                    WorkspaceFactoryType type = WorkspaceFactoryType.Raster;
                    switch (item)
                    {
                    case "shp":
                        type = WorkspaceFactoryType.Shapefile;
                        break;

                    case "bmp":
                    case "png":
                    case "sid":
                    case "tif":
                        type = WorkspaceFactoryType.Raster;
                        break;
                    }

                    var dynLayer =
                        await MapHandler.AddFileDatasetToDynamicMapServiceLayer(type,
                                                                                System.IO.Path.GetDirectoryName(openFileDialog.FileName),
                                                                                new List <string>(openFileDialog.SafeFileNames), this.MyMapView.Map.Layers.Count);

                    // Add the dynamic map service layer to the map
                    if (dynLayer != null)
                    {
                        dynLayer.DisplayName = dynLayer.DynamicLayerInfos[0].Name;
                        MyMapView.Map.Layers.Add(dynLayer);
                        MyMapView.SetView(dynLayer.FullExtent.Extent.Expand(0.5));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
        }