***由于在视图布局上加载.mxd文件会直接会覆盖原数据,所以需要进行一定的处理,一下有两种方式:
方法一、使用FeatureLayer的render方法对每个图层分布进行渲染:***
//获取图层
List<ILayer> layerlist = new List<ILayer>();
for (int i = 0; i < _pageLayoutControl.ActiveView.FocusMap.LayerCount; i++)
{
layerlist.Add(_pageLayoutControl.ActiveView.FocusMap.get_Layer(i));
}
_pageLayoutControl.LoadMxFile(filePath);
IMapDocument mapDocument = new MapDocument();
mapDocument.Open(filePath, "");
//对插入控件进行引用,但是这里没有对控件的样式进行处理
IGraphicsContainer graphicsContainer = mapDocument.PageLayout as IGraphicsContainer;
graphicsContainer.Reset();
IElement pElement = graphicsContainer.Next();
_pageLayoutControl.GraphicsContainer.DeleteAllElements();
while (pElement != null)//循环遍历
{
if (pElement is IMapSurroundFrame)//地图整饰要素
{
IMapSurroundFrame pMapSurroundFrame = pElement as IMapSurroundFrame;
IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
IEnvelope enlp = mapDocument.PageLayout.Page.PrintableBounds;
pElement.Geometry = enlp as IGeometry;
_pageLayoutControl.GraphicsContainer.AddElement(pElement, 0);
}
pElement = graphicsContainer.Next();
}
//遍历图层进行渲染
for (int i = 0; i < mapDocument.ActiveView.FocusMap.LayerCount; i++)
{
IGeoFeatureLayer featureLayer = mapDocument.ActiveView.FocusMap.get_Layer(i) as IGeoFeatureLayer;
IGeoFeatureLayer featureLayer1 = layerlist.Find(c => c.Name == featureLayer.Name) as IGeoFeatureLayer;
if (featureLayer != null)
{
featureLayer1.Renderer = featureLayer.Renderer;
}
}
_pageLayoutControl.Refresh();
方法二、先加载整个的.mxd模板,在对模板进行遍历,对每个图层的FeatureClass重新赋值:
//获取图层
List<ILayer> layerlist = new List<ILayer>();
for (int i = 0; i < _pageLayoutControl.ActiveView.FocusMap.LayerCount; i++)
{
layerlist.Add(_pageLayoutControl.ActiveView.FocusMap.get_Layer(i));
}
_pageLayoutControl.ActiveView.FocusMap.ClearLayers();
_pageLayoutControl.LoadMxFile(filePath);
for (int i = 0; i < _pageLayoutControl.ActiveView.FocusMap.LayerCount; i++)
{
IFeatureLayer featureLayer = _pageLayoutControl.ActiveView.FocusMap.get_Layer(i) as IFeatureLayer;
IFeatureLayer featureLayer1 = layerlist.Find(c => c.Name == Global.TableRegInfo.Instance.LayerCollection.Find(b => b.TableName == featureLayer.Name)?.LayerName) as IFeatureLayer;
if (featureLayer != null)
{
featureLayer.FeatureClass = featureLayer1.FeatureClass;
}
}
_pageLayoutControl.Refresh();
比较:方法二优点是可以不用对模板的引用控件进行处理。