Silverlight 服务器端打印出现错误

printParameters 类有两个构造方法。其中一个是输入打印的map;另一个是输入打印的图层和打印的范围。当需要手动指定打印输入范围的时候就需要使用方法二的构造函数。

在Silverlight api的sample中给出了客户端和服务器端打印的sample。使用服务器端打印的sample的时候,当输入的图层设置为
 PrintParameters printParameters = new PrintParameters(pMapLayer.Layers,MyMap.Extent)
 
的时候,会出现错误信息Layer was already associtated with another map
已邀请:

江民彬

赞同来自:

【解决办法】:
 

该问题的解决方式是通过代码,自己编写一个获取当前map中的所有图层的枚举对象。避开使用map对象去获取所有的图层。
class MapLayers : IEnumerable<Layer>
{
List<Layer> myList = new List<Layer>();

public void add(int index, Layer layer)
{
myList.Insert(index, layer);
}

public IEnumerator<Layer> GetEnumerator()
{
return myList.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}


完整的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Shapes;
using System.ComponentModel;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Printing;
using System.Windows.Controls.Primitives;

namespace PrintProblem
{
public partial class MainPage : UserControl
{
PrintTask printTask;
PrintParameters printParameters;
MapLayers mapLayers;
ArcGISDynamicMapServiceLayer Layer1;
FeatureLayer Layer2;
ArcGISTiledMapServiceLayer Layer3;

public MainPage()
{
InitializeComponent();
mapLayers = new MapLayers();
Layer1 = new ArcGISDynamicMapServiceLayer();
Layer1.Url = http://sampleserver3.arcgisonl ... er%3B

Layer2 = new FeatureLayer();
Layer2.Url = http://sampleserver3.arcgisonl ... er%3B

Layer3 = new ArcGISTiledMapServiceLayer();
Layer3.Url = http://services.arcgisonline.c ... er%3B

printTask = new PrintTask(http://sampleserver6.arcgisonl ... 20Task);
printTask.DisableClientCaching = true;
printTask.ExecuteCompleted += printTask_PrintCompleted;
printTask.GetServiceInfoCompleted += printTask_GetServiceInfoCompleted;
printTask.GetServiceInfoAsync();


}

private void printTask_GetServiceInfoCompleted(object sender, ServiceInfoEventArgs e)
{
LayoutTemplates.ItemsSource = e.ServiceInfo.LayoutTemplates;
Formats.ItemsSource = e.ServiceInfo.Formats;
}

private void printTask_PrintCompleted(object sender, PrintEventArgs e)
{
System.Windows.Browser.HtmlPage.Window.Navigate(e.PrintResult.Url, _blank);
}

private void ExportMap_Click(object sender, RoutedEventArgs e)
{
if (printTask == null || printTask.IsBusy) return;

mapLayers.add(0, Layer3);
mapLayers.add(1, Layer2);
mapLayers.add(2, Layer1);

PrintParameters printParameters = new PrintParameters(mapLayers, MyMap.Extent)
{
ExportOptions = new ExportOptions() { Dpi = 96, OutputSize = new Size(MyMap.ActualWidth, MyMap.ActualHeight) },
LayoutTemplate = (string)LayoutTemplates.SelectedItem ?? string.Empty,
Format = (string)Formats.SelectedItem
};

printTask.ExecuteAsync(printParameters);
}

}

class MapLayers : IEnumerable<Layer>
{
List<Layer> myList = new List<Layer>();

public void add(int index, Layer layer)
{
myList.Insert(index, layer);
}

public IEnumerator<Layer> GetEnumerator()
{
return myList.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}

要回复问题请先登录注册