Silverlight4:Devexpress Report
时间:2011-04-04 来源:davin
我不知道在silvelright中对于report报表有什么好的解决方案,除了devexpress,telerik这些第3方的公司开发的silverlight 报表控件之外。我在google疯狂检索,没有找到像样的开源的解决方案。在一番比较群横还是选择devexpress的报表控件。
下面的内容就是利用devexpress report来完成一个报表:
具体来将有2种方式
1)报表和silverlight app在2个不同windows 窗口
2)报表嵌在silverlight page中
无论哪种方式都是看,具体的需求。
首先在silverlight host web里面加入Report.svc:
/// <summary>
/// IReportService
/// </summary>
[ServiceContract]
public interface IReportService : DevExpress.XtraReports.Service.IReportService {
[OperationContract]
DocumentId GetReportsByName(string reportName);
}
/// <summary>
/// ReportService
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ReportService : DevExpress.XtraReports.Service.ReportService, IReportService {
public ReportService() {
}
const string filePath = "~/Reports";
public byte[] GetReportFile(string path)
{
return File.ReadAllBytes(path);
}
private static string GetAbsulotePath(string path)
{
return "\\" + path;
}
public DocumentId GetReportsByName(string reportName)
{
//XtraReport report = new XtraReport();
string path=HttpContext.Current.Server.MapPath(filePath + "//" + reportName+".repx");
byte[] bytes=GetReportFile(path);
XtraReport report = XtraReport.FromStream(new MemoryStream(bytes), true);
//report.DataSource = GetData();
DocumentId doc = StartBuild(report);
return doc;
}
}
Silverlight Project 中的页面:xaml
<Grid x:Name="LayoutRoot">
<StackPanel Orientation="Vertical">
<Button Content="button" Width="50" Height="20" Click="Button_Click"></Button>
<my:DocumentPreview Name="documentPreview1" />
</StackPanel>
</Grid>
放置一个DocumentPreView,当button点击,将报表通过DocumentPreView渲染,mainPage.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
ReportService.ReportServiceClient client = new ReportServiceClient();
client.GetReportsByNameCompleted += new EventHandler<GetReportsByNameCompletedEventArgs>(client_GetReportsByNameCompleted);
client.GetReportsByNameAsync("XtraReport1");
}
void client_GetReportsByNameCompleted(object sender, GetReportsByNameCompletedEventArgs e)
{
ReportPreviewModel previewModel = new ReportPreviewModel(ServiceUri.AbsoluteUri);
previewModel.ProcessDocument(e.Result);
documentPreview1.Model = previewModel;
//throw new NotImplementedException();
}
const string RelativeServiceUrl = "/ReportService.svc";
static Uri serviceAbsoluteUri;
internal static Uri ServiceUri
{
get
{
if (serviceAbsoluteUri == null)
serviceAbsoluteUri = new Uri(Application.Current.Host.Source, ".." + RelativeServiceUrl);
return serviceAbsoluteUri;
}
}
剩下要注意的是ServiceReference.ClientConfig:
<endpoint address="http://localhost:53609/ReportService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IReportService"
contract="ReportService.IReportService" name="BasicHttpBinding_IReportService" />
contract是 ReportService.IReportService
这样就是一个简单的devexpress report
环境:visual studio 2010,silverlight4 DevExpress v10.1