要做WPF与ArcGIS Engine的开发,首先需要再WPF中添加Map控件。我使用的是AE 10 ,VS 2010。步骤如下:
1.打开VS2010,创建WPF应用程序,我把它命名为MapTest;
2.添加程序集引用:
ESRI.ArcGIS.AxControls;
ESRI.ArcGIS.System;
WindowsFormsIntegration;
System.Windows.Forms
3.添加命名空间:
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
然后添加窗体加载事件
Loaded="Window_Loaded"
4.添加WindowsFormHost
<my:WindowsFormsHost Margin="2" x:Name="TocControlHost" />
<my:WindowsFormsHost Grid.Column="1" x:Name="MapControlHost" Margin="308,6,299,33" >
<my:WindowsFormsHost x:Name="ToolControlHost" Margin="2" />
5.编辑Xml的C#代码。后台代码部分我参考了http://blog.csdn.net/flexmapserver/article/details/5868882 熊显权的博客,但是他的代码有些地方我在使用时不知道为什么,无法正常显示,所以根据我的情况稍微进行了修改。
首先是添加引用:
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
声明控件
AxMapControl mapControl;
AxTOCControl tocControl;
AxToolbarControl toolbarControl;
private void CreateEngineControls()
{
mapControl = new AxMapControl();
this.MapControlHost.Child = mapControl;
tocControl = new AxTOCControl();
this.TocControlHost.Child = tocControl;
toolbarControl = new AxToolbarControl();
this.ToolControlHost.Child = toolbarControl;
}
6.向toolbarControl添加内容
private void AddtoolbarControlTest()
{
tocControl.SetBuddyControl(mapControl);
toolbarControl.SetBuddyControl(mapControl);
toolbarControl.AddItem("esriControlToolsGeneric.ControlsOpenDocCommand");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPageZoomInTool");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPageZoomOutTool");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPagePanTool");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPageZoomWholePageCommand");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand");
toolbarControl.AddItem("esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand");
toolbarControl.AddItem("esriControlToolsMapNavigation.ControlsMapZoomInTool");
toolbarControl.AddItem("esriControlToolsMapNavigation.ControlsMapZoomOutTool");
toolbarControl.AddItem("esriControlToolsMapNavigation.ControlsMapPanTool");
toolbarControl.AddItem("esriControlToolsMapNavigation.ControlsMapFullExtentCommand");
}
在此处熊显权的博客中是这样定义的:
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);
}
我在使用他的方法时,toolbarControl无法正常显示添加的工具,而且
toolbarControl.BackColor =Color.FromArgb (245, 245, 220);
VS提示我此方法没有任何重载采用3个参数。因此我没有添加backcolor。 如果toolbarControl还是无法正常显示添加的功能项,可以试试下面的添加方法:
private void AddIntems()
{
// 增加打开档命令
string progID;
progID = "esriControlToolsGeneric.ControlsOpenDocCommand";
mytool.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
// 增加PageLayout导航命令
progID = "esriControlToolsPageLayout.ControlsPageZoomInTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomOutTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPagePanTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomWholePageCommand";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
// 增加地图导航命令
progID = "esriControlToolsMapNavigation.ControlsMapZoomInTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapZoomOutTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapPanTool";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapFullExtentCommand";
mytool.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
}
7.最后,在App中添加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);
}
}
}
到此,就成功的将MapControl toolbarControl tocControl添加到了WPF的窗体中,就可以正常的进行下面的开发了。
熊显权的博客地址: http://blog.csdn.net/flexmapserver/article/details/5868882