使用XAML在WPF项目中承载ArcGIS Engine地图控件开发

简介: 原文 http://blog.csdn.net/flexmapserver/article/details/5868882 用Windows Form进行ArcGIS Engine二次开发时常见的形式,当然也可以用WPF来进行ArcEngine的二次开发。

原文 http://blog.csdn.net/flexmapserver/article/details/5868882

用Windows Form进行ArcGIS Engine二次开发时常见的形式,当然也可以用WPF来进行ArcEngine的二次开发。

        由于WPF很方便承载Windows Form控件,而Map Control、Toolbar Control、TOC Control等都是.NET 控件,当然也可以用XAML来承载ArcEngine的这些控件来开发了。

        下面简单记述开发步骤:

        1.打开VS2008,创建WPF应用程序;

        2.添加程序集引用:

  • ESRI.ArcGIS.AxControls:包含地图控件
  • ESRI.ArcGIS.System:包含ArcGIS Engine license初始化类
  • WindowsFormsIntegration:包含WindowsFormsHost,用来在WPF中承载Windows控件
  • System.Windows.Forms

        3.在XAML中添加名称空间:

xmlns:controlHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

添加初始化事件Loaded="Window_Loaded"

         4.在XAML中添加Map Control、Toolbar Control、TOC Control控件,最后你的XAML代码看起来是这样:

[c-sharp] view plain copy print ?
  1. <Window x:Class="WPFMapViewer.MapWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     Title="MapViewer Hosted in WPF" Height="433.29" Width="559.944" Loaded="Window_Loaded" Background="Beige"  
  5.     MaxHeight="768" MaxWidth="1024"  
  6.     xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration">  
  7.     <Grid>          
  8.         <my:WindowsFormsHost Name="mapHost" Margin="174,30,0,22" />  
  9.         <my:WindowsFormsHost Margin="0,30,0,22" Name="tocHost" HorizontalAlignment="Left" Width="173" />  
  10.         <my:WindowsFormsHost Height="30" Name="toolbarHost" VerticalAlignment="Top" Margin="0,0,252,0" />  
  11.         <Button Content="MyZoomInBoxTool" x:Name="MyZoomInBoxTool" Click="MyZoomInBox_Click" HorizontalAlignment="Right" Width="120" Height="30" VerticalAlignment="Top" Panel.ZIndex="1" Margin="0,0,7.056,0"></Button>  
  12.         <TextBlock Height="23.75" VerticalAlignment="Bottom" Name="textBlock1" Margin="0,0,7.056,0">Ready</TextBlock>  
  13.         <Button x:Name="DrawCircleTool" Height="23" HorizontalAlignment="Right" Margin="0,5,153,0" VerticalAlignment="Top" Width="75" Click="DrawCircleTool_Click">DrawCircle</Button>  
  14.     </Grid>  
  15. </Window>  

         5.编辑XAML的C#代码,添加Map Control、Toolbar Control、TOC Control三个变量,即

             AxMapControl mapControl;
             AxToolbarControl toolbarControl;
             AxTOCControl tocControl;

            并在初始化窗口的下面添加对这三个控件变量的创建,即

[c-sharp] view plain copy print ?
  1. private void CreateEngineControls ()  
  2. {      
  3.     mapControl = new AxMapControl ();  
  4.     mapHost.Child = mapControl;  
  5.     toolbarControl = new AxToolbarControl ();  
  6.     toolbarHost.Child = toolbarControl;  
  7.     tocControl = new AxTOCControl ();  
  8.     tocHost.Child = tocControl;  
  9. }  

 

        6.在Window_Loaded事件中加载上述三个控件,如下:

       

[c-sharp] view plain copy print ?
  1. private void LoadMap ()  
  2. {  
  3.          //将TOC控件、Toolbar控件和地图控件绑定  
  4.     tocControl.SetBuddyControl (mapControl);              
  5.     toolbarControl.SetBuddyControl (mapControl);  
  6.   
  7.     //添加放大、缩小、打开地图文档等命令到Toolbar工具栏  
  8.     toolbarControl.AddItem ("esriControls.ControlsOpenDocCommand");  
  9.     toolbarControl.AddItem ("esriControls.ControlsAddDataCommand");  
  10.     toolbarControl.AddItem ("esriControls.ControlsSaveAsDocCommand");  
  11.     toolbarControl.AddItem ("esriControls.ControlsMapNavigationToolbar");  
  12.     toolbarControl.AddItem ("esriControls.ControlsMapIdentifyTool");  
  13.           
  14.     //设置工具栏的外观  
  15.     toolbarControl.BackColor =Color.FromArgb (245, 245, 220);         
  16. }  

 

        7.将上述代码连起来,你的代码看起来是这样:

        

[c-sharp] view plain copy print ?
  1. public partial class MapWindow: Window  
  2. {  
  3.     AxMapControl mapControl;  
  4.     AxToolbarControl toolbarControl;  
  5.     AxTOCControl tocControl;  
  6.   
  7.     public MapWindow ()  
  8.     {  
  9.         InitializeComponent ();  
  10.         CreateEngineControls ();  
  11.     }  
  12.           
  13.     private void CreateEngineControls ()  
  14.     {             
  15.              mapControl = new AxMapControl ();  
  16.         mapHost.Child = mapControl;  
  17.   
  18.              toolbarControl = new AxToolbarControl ();  
  19.         toolbarHost.Child = toolbarControl;  
  20.   
  21.         tocControl = new AxTOCControl ();  
  22.         tocHost.Child = tocControl;  
  23.     }  
  24.   
  25.     private void LoadMap ()  
  26.     {  
  27.         //将TOC控件、Toolbar控件和地图控件绑定  
  28.         tocControl.SetBuddyControl (mapControl);              
  29.         toolbarControl.SetBuddyControl (mapControl);  
  30.   
  31.         //添加放大、缩小、打开地图文档等命令到Toolbar工具栏  
  32.         toolbarControl.AddItem ("esriControls.ControlsOpenDocCommand");  
  33.         toolbarControl.AddItem ("esriControls.ControlsAddDataCommand");  
  34.         toolbarControl.AddItem ("esriControls.ControlsSaveAsDocCommand");  
  35.         toolbarControl.AddItem ("esriControls.ControlsMapNavigationToolbar");  
  36.         toolbarControl.AddItem ("esriControls.ControlsMapIdentifyTool");  
  37.               
  38.         //设置工具栏的外观  
  39.         toolbarControl.BackColor =Color.FromArgb (245, 245, 220);         
  40.     }  
  41.   
  42.     private void Window_Loaded (object sender, RoutedEventArgs e)  
  43.     {  
  44.         LoadMap ();              
  45.     }         
  46. }  

         8.ArcEngine的二次开发当然要License啦,在Windwos From的开发中可以用License控件来进行许可证的初始化,在这里就只能用代码在App.XAML.cs中初始化License了。

          代码如下:

         

[c-sharp] view plain copy print ?
  1. public partial class App: Application  
  2. {  
  3.     public App ()  
  4.          {  
  5.          InitializeEngineLicense ();  
  6.     }  
  7.   
  8.     private void InitializeEngineLicense ()  
  9.     {  
  10.         AoInitialize aoi = new AoInitializeClass ();  
  11.   
  12.         esriLicenseProductCode productCode = esriLicenseProductCode.esriLicenseProductCodeEngine;  
  13.         if (aoi.IsProductCodeAvailable (productCode) == esriLicenseStatus.esriLicenseAvailable)  
  14.         {  
  15.             aoi.Initialize (productCode);  
  16.         }  
  17.     }  
  18. }  

          9.在WPF中添加自定义工具,如在视图上画圆形的工具,添加DrawCircleToolClass类,如下:

           

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Runtime.InteropServices;  
  6. using ESRI.ArcGIS.ADF.CATIDs;  
  7. using ESRI.ArcGIS.Controls;  
  8. using ESRI.ArcGIS.Geometry;  
  9. using ESRI.ArcGIS.Display;  
  10. using ESRI.ArcGIS.Carto;  
  11. using ESRI.ArcGIS.ADF.BaseClasses;  
  12.   
  13. namespace WPFMapViewer.MapTool  
  14. {  
  15.     [ClassInterface(ClassInterfaceType.None)]  
  16.     [Guid("48BD64CD-3369-47FC-8EC5-94A5B644E8DA")]  
  17.   
  18.     public class DrawCircleToolClass : BaseTool  
  19.     {  
  20.         [ComRegisterFunction()]  
  21.         [ComVisible(false)]  
  22.         static void RegisterFunction(Type registerType)  
  23.         {  
  24.             ArcGISCategoryRegistration(registerType);  
  25.         }  
  26.   
  27.         [ComUnregisterFunction()]  
  28.         [ComVisible(false)]  
  29.         static void UnregisterFunction(Type registerType)  
  30.         {  
  31.             ArcGISCategoryUnregistration(registerType);  
  32.         }  
  33.   
  34.         private static void ArcGISCategoryRegistration(Type registerType)  
  35.         {  
  36.             string regKey = string.Format("HKEY_CLASSES_ROOT//CLSID//{{{0}}}", registerType.GUID);  
  37.             ControlsCommands.Register(regKey);  
  38.         }  
  39.   
  40.         private static void ArcGISCategoryUnregistration(Type registerType)  
  41.         {  
  42.             string regKey = string.Format("HKEY_CLASSES_ROOT//CLSID//{{{0}}}", registerType.GUID);  
  43.             ControlsCommands.Unregister(regKey);  
  44.         }  
  45.   
  46.         private IHookHelper m_hookHelper;  
  47.         private INewCircleFeedback m_feedBack;  
  48.   
  49.         private IPoint m_point;  
  50.         private Boolean m_isMouseDown;  
  51.   
  52.         private IDisplayFeedback displayFeedback = null;  
  53.   
  54.         public DrawCircleToolClass()  
  55.         {  
  56.             m_hookHelper = new HookHelperClass();  
  57.         }  
  58.   
  59.         ~DrawCircleToolClass()  
  60.         {  
  61.             m_hookHelper = null;  
  62.         }  
  63.   
  64.         public override void OnCreate(object hook)  
  65.         {  
  66.             m_hookHelper.Hook = hook;  
  67.         }  
  68.   
  69.         public override bool Enabled  
  70.         {  
  71.             get  
  72.             {  
  73.                 if (m_hookHelper.FocusMap == nullreturn false;  
  74.                 return (m_hookHelper.FocusMap.LayerCount > 0);  
  75.             }  
  76.         }  
  77.   
  78.         public override void OnMouseDown(int Button, int Shift, int X, int Y)  
  79.         {  
  80.             //Create a point in map coordinates  
  81.             IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;  
  82.             m_point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);  
  83.             displayFeedback = new NewCircleFeedbackClass();  
  84.             m_isMouseDown = true;  
  85.   
  86.         }  
  87.   
  88.         public override void OnMouseMove(int button, int shift, int x, int y)  
  89.         {  
  90.             if (!m_isMouseDown) return;  
  91.   
  92.             IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;  
  93.             if (m_feedBack == null)  
  94.             {  
  95.                 m_feedBack = new NewCircleFeedbackClass();  
  96.                 m_feedBack.Display = pActiveView.ScreenDisplay;  
  97.                 m_feedBack.Start(m_point);  
  98.             }  
  99.   
  100.             m_feedBack.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y));  
  101.         }  
  102.   
  103.         public override void OnMouseUp(int button, int shift, int x, int y)  
  104.         {  
  105.             if (!m_isMouseDown) return;  
  106.             IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;  
  107.             ICircularArc pCircle;  
  108.   
  109.             pCircle = m_feedBack.Stop();  
  110.   
  111.             if (pCircle.Radius < 0.5)  
  112.             {  
  113.                 m_feedBack = null;  
  114.                 m_isMouseDown = false;  
  115.             }  
  116.   
  117.             if (pCircle != null)  
  118.             {  
  119.                 ISegmentCollection segCollection = new PolygonClass() as ISegmentCollection;  
  120.                 object o = Type.Missing;  
  121.                 segCollection.AddSegment(pCircle as ISegment, ref o, ref o);  
  122.                 IPolygon polygon = segCollection as IPolygon;  
  123.   
  124.                 IRgbColor rgbColor = new RgbColorClass();  
  125.                 rgbColor.Red = 255;  
  126.   
  127.                 IColor color = rgbColor;  
  128.                 ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();  
  129.                 simpleFillSymbol.Color = color;  
  130.   
  131.                 IElement element;  
  132.                 IPolygonElement polygonElement = new PolygonElementClass();  
  133.                 element = polygonElement as IElement;  
  134.                 IFillShapeElement fillShapeElement = polygonElement as IFillShapeElement;  
  135.                 fillShapeElement.Symbol = simpleFillSymbol;  
  136.                 element.Geometry = polygon as IGeometry;  
  137.   
  138.                 IGraphicsContainer pGraphicContainer = pActiveView.GraphicsContainer;  
  139.                 pGraphicContainer.AddElement(element, 0);  
  140.                 pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, nullnull);  
  141.             }  
  142.   
  143.             m_feedBack = null;  
  144.             m_isMouseDown = false;  
  145.         }        
  146.          
  147.     }  
  148. }  

           注意:要添加ArcEngine的相关程序集的引用, 如:ESRI.ArcGIS.ADF,ESRI.ArcGIS.Carti,ESRI.ArcGIS.Controls,ESRI.ArcGIS.Display,ESRI.ArcGIS.Geometry,ESRI.ArcGIS.SystemUI

          10.在XAML中添加一个Button,并添加一个事件,DrawCircleTool_Click,在C#代码中添加该事件的代码如下:

           

[c-sharp] view plain copy print ?
  1. //绘制圆形  
  2. private void DrawCircleTool_Click(object sender, RoutedEventArgs e)  
  3. {  
  4.     ICommand DrawCircleTool = new DrawCircleToolClass();  
  5.     DrawCircleTool.OnCreate(mapControl.Object);  
  6.     mapControl.CurrentTool = (ITool)DrawCircleTool;  
  7. }  

         11.最后执行程序,可以在加载地图文档,放大、缩小地图,也可以用自定义的画圆的工具在地图画圆。

目录
相关文章
|
4月前
|
XML 开发框架 .NET
|
6月前
|
人工智能 NoSQL 定位技术
标准地图的矢量模板,ArcGIS可打开
标准地图的矢量模板,ArcGIS可打开
87 0
|
6月前
|
IDE C# 开发工具
2000条你应知的WPF小姿势 基础篇<40-44 启动关闭,Xaml,逻辑树>
2000条你应知的WPF小姿势 基础篇<40-44 启动关闭,Xaml,逻辑树>
31 0
|
4月前
|
人工智能 编解码 定位技术
ArcGIS导出AI或EPS格式的地图图片并在Adobe Illustrator中继续编辑
ArcGIS导出AI或EPS格式的地图图片并在Adobe Illustrator中继续编辑
|
4月前
|
存储 定位技术
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
|
4月前
|
编解码 定位技术 Python
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
|
5月前
|
程序员 C# 异构计算
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
55 0
|
5月前
|
C# 开发者
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
125 36
|
8月前
|
C#
WPF技术之Xaml Window
WPF Window 是一个 WPF 窗口类,它具有许多属性枚举可以控制窗口的外观和行为。
76 0
WPF技术之Xaml Window
|
8月前
|
XML 数据格式 C++
WPF-疑难问题-xaml编码导致中文字符编译无效
WPF-疑难问题-xaml编码导致中文字符编译无效
108 0