使用XAML在WPF项目中承载ArcGIS Engine地图控件开发
原文 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代码看起来是这样:
- <Window x:Class="WPFMapViewer.MapWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MapViewer Hosted in WPF" Height="433.29" Width="559.944" Loaded="Window_Loaded" Background="Beige"
- MaxHeight="768" MaxWidth="1024"
- xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration">
- <Grid>
- <my:WindowsFormsHost Name="mapHost" Margin="174,30,0,22" />
- <my:WindowsFormsHost Margin="0,30,0,22" Name="tocHost" HorizontalAlignment="Left" Width="173" />
- <my:WindowsFormsHost Height="30" Name="toolbarHost" VerticalAlignment="Top" Margin="0,0,252,0" />
- <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>
- <TextBlock Height="23.75" VerticalAlignment="Bottom" Name="textBlock1" Margin="0,0,7.056,0">Ready</TextBlock>
- <Button x:Name="DrawCircleTool" Height="23" HorizontalAlignment="Right" Margin="0,5,153,0" VerticalAlignment="Top" Width="75" Click="DrawCircleTool_Click">DrawCircle</Button>
- </Grid>
- </Window>
5.编辑XAML的C#代码,添加Map Control、Toolbar Control、TOC Control三个变量,即
AxMapControl mapControl;
AxToolbarControl toolbarControl;
AxTOCControl tocControl;
并在初始化窗口的下面添加对这三个控件变量的创建,即
- private void CreateEngineControls ()
- {
- mapControl = new AxMapControl ();
- mapHost.Child = mapControl;
- toolbarControl = new AxToolbarControl ();
- toolbarHost.Child = toolbarControl;
- tocControl = new AxTOCControl ();
- tocHost.Child = tocControl;
- }
6.在Window_Loaded事件中加载上述三个控件,如下:
- private void LoadMap ()
- {
- //将TOC控件、Toolbar控件和地图控件绑定
- tocControl.SetBuddyControl (mapControl);
- toolbarControl.SetBuddyControl (mapControl);
- //添加放大、缩小、打开地图文档等命令到Toolbar工具栏
- toolbarControl.AddItem ("esriControls.ControlsOpenDocCommand");
- toolbarControl.AddItem ("esriControls.ControlsAddDataCommand");
- toolbarControl.AddItem ("esriControls.ControlsSaveAsDocCommand");
- toolbarControl.AddItem ("esriControls.ControlsMapNavigationToolbar");
- toolbarControl.AddItem ("esriControls.ControlsMapIdentifyTool");
- //设置工具栏的外观
- toolbarControl.BackColor =Color.FromArgb (245, 245, 220);
- }
7.将上述代码连起来,你的代码看起来是这样:
- public partial class MapWindow: Window
- {
- AxMapControl mapControl;
- AxToolbarControl toolbarControl;
- AxTOCControl tocControl;
- public MapWindow ()
- {
- InitializeComponent ();
- CreateEngineControls ();
- }
- private void CreateEngineControls ()
- {
- mapControl = new AxMapControl ();
- mapHost.Child = mapControl;
- toolbarControl = new AxToolbarControl ();
- toolbarHost.Child = toolbarControl;
- tocControl = new AxTOCControl ();
- tocHost.Child = tocControl;
- }
- private void LoadMap ()
- {
- //将TOC控件、Toolbar控件和地图控件绑定
- tocControl.SetBuddyControl (mapControl);
- toolbarControl.SetBuddyControl (mapControl);
- //添加放大、缩小、打开地图文档等命令到Toolbar工具栏
- toolbarControl.AddItem ("esriControls.ControlsOpenDocCommand");
- toolbarControl.AddItem ("esriControls.ControlsAddDataCommand");
- toolbarControl.AddItem ("esriControls.ControlsSaveAsDocCommand");
- toolbarControl.AddItem ("esriControls.ControlsMapNavigationToolbar");
- toolbarControl.AddItem ("esriControls.ControlsMapIdentifyTool");
- //设置工具栏的外观
- toolbarControl.BackColor =Color.FromArgb (245, 245, 220);
- }
- private void Window_Loaded (object sender, RoutedEventArgs e)
- {
- LoadMap ();
- }
- }
8.ArcEngine的二次开发当然要License啦,在Windwos From的开发中可以用License控件来进行许可证的初始化,在这里就只能用代码在App.XAML.cs中初始化License了。
代码如下:
- public partial class App: Application
- {
- public App ()
- {
- InitializeEngineLicense ();
- }
- private void InitializeEngineLicense ()
- {
- AoInitialize aoi = new AoInitializeClass ();
- esriLicenseProductCode productCode = esriLicenseProductCode.esriLicenseProductCodeEngine;
- if (aoi.IsProductCodeAvailable (productCode) == esriLicenseStatus.esriLicenseAvailable)
- {
- aoi.Initialize (productCode);
- }
- }
- }
9.在WPF中添加自定义工具,如在视图上画圆形的工具,添加DrawCircleToolClass类,如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using ESRI.ArcGIS.ADF.CATIDs;
- using ESRI.ArcGIS.Controls;
- using ESRI.ArcGIS.Geometry;
- using ESRI.ArcGIS.Display;
- using ESRI.ArcGIS.Carto;
- using ESRI.ArcGIS.ADF.BaseClasses;
- namespace WPFMapViewer.MapTool
- {
- [ClassInterface(ClassInterfaceType.None)]
- [Guid("48BD64CD-3369-47FC-8EC5-94A5B644E8DA")]
- public class DrawCircleToolClass : BaseTool
- {
- [ComRegisterFunction()]
- [ComVisible(false)]
- static void RegisterFunction(Type registerType)
- {
- ArcGISCategoryRegistration(registerType);
- }
- [ComUnregisterFunction()]
- [ComVisible(false)]
- static void UnregisterFunction(Type registerType)
- {
- ArcGISCategoryUnregistration(registerType);
- }
- private static void ArcGISCategoryRegistration(Type registerType)
- {
- string regKey = string.Format("HKEY_CLASSES_ROOT//CLSID//{{{0}}}", registerType.GUID);
- ControlsCommands.Register(regKey);
- }
- private static void ArcGISCategoryUnregistration(Type registerType)
- {
- string regKey = string.Format("HKEY_CLASSES_ROOT//CLSID//{{{0}}}", registerType.GUID);
- ControlsCommands.Unregister(regKey);
- }
- private IHookHelper m_hookHelper;
- private INewCircleFeedback m_feedBack;
- private IPoint m_point;
- private Boolean m_isMouseDown;
- private IDisplayFeedback displayFeedback = null;
- public DrawCircleToolClass()
- {
- m_hookHelper = new HookHelperClass();
- }
- ~DrawCircleToolClass()
- {
- m_hookHelper = null;
- }
- public override void OnCreate(object hook)
- {
- m_hookHelper.Hook = hook;
- }
- public override bool Enabled
- {
- get
- {
- if (m_hookHelper.FocusMap == null) return false;
- return (m_hookHelper.FocusMap.LayerCount > 0);
- }
- }
- public override void OnMouseDown(int Button, int Shift, int X, int Y)
- {
- //Create a point in map coordinates
- IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;
- m_point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
- displayFeedback = new NewCircleFeedbackClass();
- m_isMouseDown = true;
- }
- public override void OnMouseMove(int button, int shift, int x, int y)
- {
- if (!m_isMouseDown) return;
- IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;
- if (m_feedBack == null)
- {
- m_feedBack = new NewCircleFeedbackClass();
- m_feedBack.Display = pActiveView.ScreenDisplay;
- m_feedBack.Start(m_point);
- }
- m_feedBack.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y));
- }
- public override void OnMouseUp(int button, int shift, int x, int y)
- {
- if (!m_isMouseDown) return;
- IActiveView pActiveView = (IActiveView)m_hookHelper.FocusMap;
- ICircularArc pCircle;
- pCircle = m_feedBack.Stop();
- if (pCircle.Radius < 0.5)
- {
- m_feedBack = null;
- m_isMouseDown = false;
- }
- if (pCircle != null)
- {
- ISegmentCollection segCollection = new PolygonClass() as ISegmentCollection;
- object o = Type.Missing;
- segCollection.AddSegment(pCircle as ISegment, ref o, ref o);
- IPolygon polygon = segCollection as IPolygon;
- IRgbColor rgbColor = new RgbColorClass();
- rgbColor.Red = 255;
- IColor color = rgbColor;
- ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
- simpleFillSymbol.Color = color;
- IElement element;
- IPolygonElement polygonElement = new PolygonElementClass();
- element = polygonElement as IElement;
- IFillShapeElement fillShapeElement = polygonElement as IFillShapeElement;
- fillShapeElement.Symbol = simpleFillSymbol;
- element.Geometry = polygon as IGeometry;
- IGraphicsContainer pGraphicContainer = pActiveView.GraphicsContainer;
- pGraphicContainer.AddElement(element, 0);
- pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
- }
- m_feedBack = null;
- m_isMouseDown = false;
- }
- }
- }
注意:要添加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#代码中添加该事件的代码如下:
- //绘制圆形
- private void DrawCircleTool_Click(object sender, RoutedEventArgs e)
- {
- ICommand DrawCircleTool = new DrawCircleToolClass();
- DrawCircleTool.OnCreate(mapControl.Object);
- mapControl.CurrentTool = (ITool)DrawCircleTool;
- }
11.最后执行程序,可以在加载地图文档,放大、缩小地图,也可以用自定义的画圆的工具在地图画圆。
使用XAML在WPF项目中承载ArcGIS Engine地图控件开发的更多相关文章
- 如何在WPF程序中使用ArcGIS Engine的控件
原文 http://www.gisall.com/html/47/122747-4038.html WPF(Windows Presentation Foundation)是美国微软公司推出.NET ...
- WPF窗体中嵌入/使用WinForm类/控件(基于.NET Core)
如题,WPF中嵌入WinForm的做法,网络上已经很多示例,都是基于.NET XXX版的. 今天King様在尝试WPF(基于.NET Core 3.1)中加入Windows.Forms.ColorDi ...
- ArcGIS Engine 笔记-控件类型
控件 MapControl Map 地图控件 PageLayouControl 布局地图控件 TOCControl 目录控件 ToolbarCo ...
- 关于WPF中承载 ArcGIS控件。
原文 http://www.cnblogs.com/zoe-j/archive/2011/05/18/2050208.html 之前就做过WPF的应用,之前承载的MapGIS的二次开发控件,今天写一下 ...
- WPF项目中解决ConfigurationManager不能用(转)
https://blog.csdn.net/MOESECSDN/article/details/78107888 在WPF项目中遇到这样的问题,做一下笔记.希望对自己和读者都有帮助. 在aap.con ...
- Windows Presentation Foundation (WPF) 项目中不支持xxx的解决
一般Windows Presentation Foundation (WPF) 项目中不支持xxx都是由于没引用相应的程序集导致,比如Windows Presentation Foundation ( ...
- WPF中不规则窗体与WebBrowser控件的兼容问题解决办法
原文:WPF中不规则窗体与WebBrowser控件的兼容问题解决办法 引言 这几天受委托开发一个网络电视项目,要求初步先使用内嵌网页形式实现视频播放和选单,以后再考虑将网页中的所有功能整合进桌面程序. ...
- GMap.Net解决方案之在WinForm和WPF中使用GMap.Net地图插件的开发
在做地理位置相关的开发时,总是面临高额地图引擎费用让大部分用户望而却步,加之地图数据又是天价,那么GMap.NET就是首选了,它本身就是开源免费,服务器可以在本地缓存,以后访问时就可以直接访问. 可以 ...
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 WPF中不规则窗体与WebBrowser控件的兼 ...
随机推荐
- svn: “sqlite: attempt to write a readonly database”
原因很可能是在svn与本地同步的时候上锁了,可能没注意在svn执行与仓库同步的时候被中断,所以锁文件没有解锁,但是这样的错误,应该不是标题上所说的错误啊??搞不懂了,以前这样的错误,cleanup都有 ...
- Qt编程之d指针与q指针
我们在Qt中可以看到两个宏Q_D和Q_Q这两个红分别是取得d指针和q指针的,d指针指向封装的私有类,q指针指向公共的类.(我的理解类似于回调,回指的意思). 为什么Qt要这样实现呢?下面几个链接中的文 ...
- 转:PHP分页技术的代码和示例
本文来自:10 Helpful PHP Pagination Scripts For Web Developers 分页是目前在显示大量结果时所采用的最好的方式.有了下面这些代码的帮助,开发人员可以在 ...
- UML--核心元素之用例
Use case 一个系统就是由各种各样的愿望组成的. 一个用例就是与参与者actor交互的,并且给参与者提供可观测的有意义的结果的一系列活动的集合. 例如你想做一顿饭吃,你需要完成煮饭和炒菜两件事情 ...
- Android Studio代码着色插件
文章将给大家分享Studio中代码高亮插件,个人觉得换个代码着色方式还是挺有必要的,起码让视觉上有个变换,感官上爽一些.就像吃惯了大鱼大肉,偶尔也来点青菜萝卜吧.以下是个人喜欢的几款,给个效果图大家看 ...
- VS 项目(c#)引用了 DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称
1. 在项目上点右键-->属性-->应用程序-->目标框架-->修改为.NET Framework 4. 而我原来的设置是.NET Framework 4 Client Pro ...
- 无心插柳OR志在必得?阿里推“来往”的意图
近年来,阿里巴巴在外围的动作确实不少,投资新浪微博.投资陌陌,配合阿里自身的一些战略调整,让人觉得这家公司似乎正在经历一场前所未有的“蜕变”.其实这也不难理解,在BAT三国演义中,任何一方都不 ...
- [android开发之内容更新类APP]二、这几日的结果
android教程即将開始 话说这开了blog之后,就一直在试用自己的app,发现.TM的真的非常不爽,不好用,好吧.本来打算放弃了.只是看到手机里还有还有一个坑,干脆又一次做一个吧. 原来的神回复A ...
- Dalvik虚拟机的运行过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8914953 在前面一篇文章中,我们分析了Dal ...
- 07_DICTIONARY_ACCESSIBILITY
07_DICTIONARY_ACCESSIBILITY 控制对系统权限的限制: TRUE 有相应系统权限,允许访问SYS下的对象. FALSE 确保拥有可以访问任何对象的系统权限,但不可以访问SYS下 ...