微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight4:Devexpress Report

我不知道在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%22
             binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IReportService"
             contract="ReportService.IReportService" name="BasicHttpBinding_IReportService" />

contract是 ReportService.IReportService

这样就是一个简单的devexpress report

环境:visual studio 2010,silverlight4 DevExpress v10.1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐