You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blazor MAUI项目中开发并集成绑定ObjectDataSource的Telerik报表?

Blazor MAUI集成Telerik报表(ObjectDataSource版)

一、开发绑定ObjectDataSource的Telerik报表

1. 准备数据源类

先定义实体模型和数据服务类,用于提供报表数据:

// 实体模型
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Stock { get; set; }
}

// 数据服务类,负责提供报表数据源
public class ProductReportService
{
    // 模拟数据,实际可替换为数据库查询或API调用
    public IEnumerable<Product> GetProducts()
    {
        return new List<Product>
        {
            new Product { Id = 1, Name = "笔记本电脑", Price = 5999.99m, Stock = 20 },
            new Product { Id = 2, Name = "无线鼠标", Price = 99.99m, Stock = 100 },
            new Product { Id = 3, Name = "机械键盘", Price = 299.99m, Stock = 50 }
        };
    }
}

将此类放在可被报表设计器引用的类库或独立项目中。

2. 创建并配置Telerik报表

  • 在Visual Studio中创建Telerik Report Library项目(或在现有项目中添加Telerik Report项)。
  • 打开报表设计器,右键报表 → Add Data Source → 选择ObjectDataSource。
  • 配置向导中点击Browse,选择包含ProductReportService的程序集,选中该类。
  • 选择数据获取方法GetProducts(),点击Finish完成数据源绑定。
  • 设计报表布局:将Id、Name等字段拖入报表的Detail区域,添加表头、表尾并设置样式,最后保存报表(如ProductsReport.trdp)。

二、集成报表到Blazor MAUI项目

1. 安装NuGet包

在Blazor MAUI项目中安装以下包:

  • Telerik.Reporting.Blazor.Maui
  • Telerik.Reporting.Services.AspNetCore
  • 引用包含ProductReportService和Product类的项目(或直接将类复制到MAUI项目)

2. 配置报表资源

将.trdp报表文件添加到MAUI项目,设置:

  • Build Action = Embedded Resource
  • Copy to Output Directory = Copy if newer

3. 注册报表服务

修改MauiProgram.cs,添加报表相关服务注册:

using Telerik.Reporting.Services;
using Telerik.Reporting.Services.AspNetCore;

var builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    });

// 注册Telerik报表核心服务
builder.Services.AddScoped<IReportServiceConfiguration>(sp =>
    new ReportServiceConfiguration
    {
        ReportingEngineConfiguration = sp.GetService<IConfiguration>(),
        HostAppId = "BlazorMAUI.ReportViewer",
        Storage = new Telerik.Reporting.Cache.FileStorage(),
        ReportSourceResolver = new UriReportSourceResolver(
            Path.Combine(AppContext.BaseDirectory, "Reports"))
    });

// 注册自定义数据源服务
builder.Services.AddScoped<ProductReportService>();

builder.Services.AddBlazorWebView();

var app = builder.Build();
app.Run();

4. 添加ReportViewer组件

创建Blazor组件ReportViewer.razor:

@page "/reportviewer"
@using Telerik.Reporting.Blazor.Maui
@using Telerik.Reporting
@inject ProductReportService ReportService

<h3>产品报表</h3>

<TelerikReportViewer Height="800px" Width="100%">
    <ReportSourceOptions>
        <ReportSource Type="UriReportSource" Uri="ProductsReport.trdp" />
    </ReportSourceOptions>
    <ObjectDataSourceSettings>
        <ObjectDataSourceResolver>
            @((typeName) =>
            {
                if (typeName == typeof(ProductReportService).FullName)
                {
                    return ReportService;
                }
                return null;
            })
        </ObjectDataSourceResolver>
    </ObjectDataSourceSettings>
</TelerikReportViewer>

5. 测试运行

启动Blazor MAUI项目,导航到/reportviewer页面,即可看到加载完成的产品报表,数据由ProductReportService提供。

内容的提问来源于stack exchange,提问作者sudathG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:22:24