在WPF中使用ArcGIS Engine

原文 http://blog.csdn.net/zzahkj/article/details/9102621

1.首先,新建一个WPF项目,添加引用ESRI.ArcGIS.AxControls、ESRI.ArcGIS.System、 ESRI.ArcGIS.Version、ESRI.ArcGIS.Controls、ESRI.ArcGIS.SystemUI,然后拖入 WindowsFormsHost控件到窗体中或者通过在Grid中添加XAML代码实现,如下:

[html] view plain copy
  1. <Grid>  
  2.        <WindowsFormsHost Height="54" HorizontalAlignment="Left" Name="windowsFormsHost1" VerticalAlignment="Top" Width="576" />  
  3.        <WindowsFormsHost Height="232" HorizontalAlignment="Left" Margin="0,60,0,0" Name="windowsFormsHost2" VerticalAlignment="Top" Width="133" />  
  4.        <WindowsFormsHost Height="232" HorizontalAlignment="Left" Margin="139,60,0,0" Name="windowsFormsHost3" VerticalAlignment="Top" Width="437" />  
  5.    </Grid>  


在WPF中使用ArcGIS Engine_第1张图片


2.为窗体添加一个WindowsLoad事件,名为Window_Loaded,可通过在Window属性里的事件Load里填写Window_Loaded

在WPF中使用ArcGIS Engine_第2张图片或者在代码中直接添加Loaded="Window_Loaded"

3.添加AE控件并建立其与WindowsFormsHost控件的联系,

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. using ESRI.ArcGIS.Controls;  
  15. using ESRI.ArcGIS.SystemUI;  
  16. using System.Windows.Forms;  
  17.   
  18. namespace WpfArcGISEngine  
  19. {  
  20.     /// <summary>  
  21.     /// MainWindow.xaml 的交互逻辑  
  22.     /// </summary>  
  23.     public partial class MainWindow : Window  
  24.     {  
  25.         AxMapControl mapControl = null;  
  26.         AxTOCControl tocControl = null;  
  27.         AxToolbarControl toolbarControl = null;  
  28.         public MainWindow()  
  29.         {  
  30.             InitializeComponent();  
  31.             CreateEngineControls();  
  32.         }  
  33.         //建立AE控件与WindowsFormsHost控件的联系  
  34.         private void CreateEngineControls()  
  35.         {  
  36.             mapControl = new AxMapControl();  
  37.             windowsFormsHost3.Child = mapControl;  
  38.             tocControl = new AxTOCControl();  
  39.             windowsFormsHost2.Child = tocControl;  
  40.             toolbarControl = new AxToolbarControl();  
  41.             windowsFormsHost1.Child = toolbarControl;  
  42.         }  
  43.         //在Window_Loaded实现TOC与MapControl控件、ToolBar与MapControl之间的绑定  
  44.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  45.         {  
  46.             tocControl.SetBuddyControl(mapControl);  
  47.             toolbarControl.SetBuddyControl(mapControl);  
  48.             ToolbarAddItems();  
  49.         }  
  50.         //ToolBar中添加工具  
  51.         private void ToolbarAddItems()  
  52.         {  
  53.             toolbarControl.AddItem(new ControlsOpenDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  
  54.             toolbarControl.AddItem(new ControlsSaveAsDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  
  55.             toolbarControl.AddItem(new ControlsAddDataCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  
  56.             toolbarControl.AddItem("esriControls.ControlsMapNavigationToolbar");  
  57.             toolbarControl.AddItem("esriControls.ControlsMapIdentifyTool");  
  58.         }  
  59.     }  
  60. }  


4.最后运行肯定不会成功,因为没有License的初始化,License初始化的方法是在App.xaml.cs中添加如下的代码实现

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Windows;  
  7. using ESRI.ArcGIS.esriSystem;  
  8. namespace WpfArcGISEngine  
  9. {  
  10.     /// <summary>  
  11.     /// App.xaml 的交互逻辑  
  12.     /// </summary>  
  13.     public partial class App : Application  
  14.     {  
  15.         protected override void OnStartup(StartupEventArgs e)  
  16.         {  
  17.             base.OnStartup(e);  
  18.             ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);  
  19.             InitializeEngineLicense();  
  20.         }  
  21.         private void InitializeEngineLicense()  
  22.         {  
  23.             AoInitialize aoi = new AoInitializeClass();  
  24.             esriLicenseProductCode productCode = esriLicenseProductCode.esriLicenseProductCodeEngine;  
  25.             if (aoi.IsProductCodeAvailable(productCode) == esriLicenseStatus.esriLicenseAvailable)  
  26.             {  
  27.                 aoi.Initialize(productCode);  
  28.             }  
  29.         }  
  30.     }  
  31. }  


现在就可以运行了,运行结果如下:

界面有点丑

在WPF中使用ArcGIS Engine_第3张图片

你可能感兴趣的:(Engine,arcgis)