天天看點

在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