天天看点

在WPF中使用ArcGIS Engine

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

<Grid>  

       <WindowsFormsHost Height="54" HorizontalAlignment="Left" Name="windowsFormsHost1" VerticalAlignment="Top" Width="576" />  

       <WindowsFormsHost Height="232" HorizontalAlignment="Left" Margin="0,60,0,0" Name="windowsFormsHost2" VerticalAlignment="Top" Width="133" />  

       <WindowsFormsHost Height="232" HorizontalAlignment="Left" Margin="139,60,0,0" Name="windowsFormsHost3" VerticalAlignment="Top" Width="437" />  

   </Grid>  

在WPF中使用ArcGIS Engine

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

在WPF中使用ArcGIS Engine

或者在代码中直接添加Loaded="Window_Loaded"

在WPF中使用ArcGIS Engine

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

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.Windows;  

using System.Windows.Controls;  

using System.Windows.Data;  

using System.Windows.Documents;  

using System.Windows.Input;  

using System.Windows.Media;  

using System.Windows.Media.Imaging;  

using System.Windows.Navigation;  

using System.Windows.Shapes;  

using ESRI.ArcGIS.Controls;  

using ESRI.ArcGIS.SystemUI;  

using System.Windows.Forms;  

namespace WpfArcGISEngine  

{  

    /// <summary>  

    /// MainWindow.xaml 的交互逻辑  

    /// </summary>  

    public partial class MainWindow : Window  

    {  

        AxMapControl mapControl = null;  

        AxTOCControl tocControl = null;  

        AxToolbarControl toolbarControl = null;  

        public MainWindow()  

        {  

            InitializeComponent();  

            CreateEngineControls();  

        }  

        //建立AE控件与WindowsFormsHost控件的联系  

        private void CreateEngineControls()  

            mapControl = new AxMapControl();  

            windowsFormsHost3.Child = mapControl;  

            tocControl = new AxTOCControl();  

            windowsFormsHost2.Child = tocControl;  

            toolbarControl = new AxToolbarControl();  

            windowsFormsHost1.Child = toolbarControl;  

        //在Window_Loaded实现TOC与MapControl控件、ToolBar与MapControl之间的绑定  

        private void Window_Loaded(object sender, RoutedEventArgs e)  

            tocControl.SetBuddyControl(mapControl);  

            toolbarControl.SetBuddyControl(mapControl);  

            ToolbarAddItems();  

        //ToolBar中添加工具  

        private void ToolbarAddItems()  

            toolbarControl.AddItem(new ControlsOpenDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  

            toolbarControl.AddItem(new ControlsSaveAsDocCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  

            toolbarControl.AddItem(new ControlsAddDataCommandClass(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);  

            toolbarControl.AddItem("esriControls.ControlsMapNavigationToolbar");  

            toolbarControl.AddItem("esriControls.ControlsMapIdentifyTool");  

    }  

}  

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

using System.Configuration;  

using System.Data;  

using ESRI.ArcGIS.esriSystem;  

    /// App.xaml 的交互逻辑  

    public partial class App : Application  

        protected override void OnStartup(StartupEventArgs e)  

            base.OnStartup(e);  

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);  

            InitializeEngineLicense();  

        private void InitializeEngineLicense()  

            AoInitialize aoi = new AoInitializeClass();  

            esriLicenseProductCode productCode = esriLicenseProductCode.esriLicenseProductCodeEngine;  

            if (aoi.IsProductCodeAvailable(productCode) == esriLicenseStatus.esriLicenseAvailable)  

            {  

                aoi.Initialize(productCode);  

            }  

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

界面有点丑

在WPF中使用ArcGIS Engine