WPF中ArcGIS控件的使用
WPF中插入ArcGIS控件
在WPF中引用ArcGIS的控件需要使用WindowsFromsHost,具体用法如下:
- 添加引用:WindowsFormsIntegration和system.windows.form
- Wpf.xaml中
<WindowsFormsHost Grid.Column="0" x:Name="mapHost">
</WindowsFormsHost>
- 后台代码中:
public AxMapControl MapControl = new AxMapControl();
/// <summary>
/// 控件初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Loaded(object sender, RoutedEventArgs e)
{
((ISupportInitialize)MapControl).BeginInit();
((ISupportInitialize)MapControl).EndInit();
}
/// <summary>
/// 内存释放,避免引起内存泄漏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Closed(object sender, EventArgs e)
{
this.MapControl.Dispose();
this.mapHost.Dispose();
}
注意:
凡是使用WindowsFormsHost的地方,在页面关闭时均须进行Dispose内存释放操作,否则容易引起内存泄漏,除非该控件贯穿程序始终,如地图控件。
Winform控件上嵌套使用WPF控件
winform控件的优先级高于WPF控件,WPF中一旦使用WindowsFormsHost插入winfrom控件,将会默认将其置顶,导致wpf控件被遮盖。但是在开发过程中有时会需要在winform控件上插入一些wpf控件,如在地图控件上布局一些小工具,比如实现如下效果:
要实现这种效果,可以再嵌套一个WindowsFromsHost,并在其中嵌套一个ElementHost,再写入WPF控件,如下所示:
<WindowsFormsHost x:Name="MapHost" Background="White" Grid.ColumnSpan="3" Grid.RowSpan="3"/>
<WindowsFormsHost Grid.Row="1" Grid.Column="2" Name="Tools"
Background="Transparent" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,5,5">
<ElementHost>
<Grid>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Top">
<Button Name="Pan" Style="{StaticResource ButtonStle}"
ToolTip="{DynamicResource PanString}"
Click="Pan_Click">
<TextBlock Text="" Style="{StaticResource BlockStyle}" />
</Button>
<Button Name="ZoomIn" Style="{StaticResource ButtonStle}"
Click="ZoomIn_Click"
ToolTip="{DynamicResource ZoomInString}">
<TextBlock Text="" Style="{StaticResource BlockStyle}"/>
</Button>
<!--其余控件代码省略-->
</StackPanel>
</Grid>
</ElementHost>
</WindowsFormsHost>