Tworzenie niestandardowego komponentu wykonawczego raportu

Niestandardowy komponent wykonawczy elementów raportu jest implementowany jako komponent Microsoft .NET Framework, używając dowolnego języka zgodnego z CLS i wywoływany przez procesora raportów w czasie działania. Właściwości komponentu w czasie działania definiujesz w środowisku projektowym, modyfikując odpowiadający mu komponent projektowy dla niestandardowego elementu raportu.

Przykładowy element w pełni zaimplementowanego niestandardowego raportu można znaleźć w sekcji SQL Server Reporting Services Product Samples.

Definicja i obiekty instancji

Przed implementacją niestandardowego elementu raportu ważne jest, aby zrozumieć różnicę między obiektami definicji a obiektami instancji. Obiekty definicyjne zapewniają reprezentację RDL niestandardowego elementu raportu, podczas gdy obiekty instancji są ocenianymi wersjami obiektów definicji. Dla każdego elementu w raporcie jest tylko jeden obiekt definicji. Przy uzyskiwaniu dostępu do właściwości obiektu definicji zawierającego wyrażenia otrzymujemy nieocenyny ciąg wyrażeń. Obiekty instancji zawierają ocenione wersje obiektów definicji i mogą mieć relację jeden do wielu z obiektem definicyjnym danego elementu. Na przykład, jeśli raport ma Tablix region danych zawierający a CustomReportItem w wierszu szczegółowym, będzie tylko jeden obiekt definicji, ale dla każdego wiersza w regionie danych będzie obiekt instancji.

Implementacja interfejsu ICustomReportItem

Aby stworzyć komponent CustomReportItem w czasie rzeczywistym, musisz zaimplementować ICustomReportItem interfejs zdefiniowany w Microsoft.ReportingServices.ProcessingCore.dll:

namespace Microsoft.ReportingServices.OnDemandReportRendering  
{  
    public interface ICustomReportItem  
    {  
        void GenerateReportItemDefinition(CustomReportItem customReportItem);  
void EvaluateReportItemInstance(CustomReportItem customReportItem);  
    }  
}  

Po wdrożeniu interfejsu ICustomReportItem zostaną wygenerowane GenerateReportItemDefinition dwa stuby metod: oraz EvaluateReportItemInstance. Metoda jest GenerateReportItemDefinition wywoływana jako pierwsza i służy do ustawiania właściwości definicji oraz tworzenia obiektu Image , który zawiera zarówno właściwości definicji, jak i instancji używane do renderowania elementu. Metoda jest EvaluateReportItemInstance wywoływana po ocenie obiektów definicji i dostarcza obiekty instancji, które będą używane do renderowania elementu.

Poniższy przykład implementacji pokazuje niestandardowy element raportu, który renderuje nazwę kontroli jako obraz.

namespace Microsoft.Samples.ReportingServices  
{  
    using System;  
    using System.Collections.Generic;  
    using System.Collections.Specialized;  
    using System.Drawing.Imaging;  
    using System.IO;  
    using System.Text;  
    using Microsoft.ReportingServices.OnDemandReportRendering;  
  
    public class PolygonsCustomReportItem : ICustomReportItem  
    {  
        #region ICustomReportItem Members  
  
        public void GenerateReportItemDefinition(CustomReportItem cri)  
        {  
            // Create the Image object that will be   
            // used to render the custom report item  
            cri.CreateCriImageDefinition();  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
        }  
  
        public void EvaluateReportItemInstance(CustomReportItem cri)  
        {  
            // Get the Image definition  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
  
            // Create the image for the custom report item  
            polygonImage.ImageInstance.ImageData = DrawImage(cri);  
        }  
  
        #endregion  
  
        /// <summary>  
        /// Creates an image of the CustomReportItem's name  
        /// </summary>  
        private byte[] DrawImage(CustomReportItem customReportItem)  
        {  
            int width = 1;          // pixels  
            int height = 1;         // pixels  
            int resolution = 75;    // dpi  
  
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Get the Font for the Text  
            System.Drawing.Font font = new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace,  
                12, System.Drawing.FontStyle.Regular);  
  
            // Get the Brush for drawing the Text  
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen);  
  
            // Get the measurements for the image  
            System.Drawing.SizeF maxStringSize = graphics.MeasureString(customReportItem.Name, font);  
            width = (int)(maxStringSize.Width + 2 * font.GetHeight(resolution));  
            height = (int)(maxStringSize.Height + 2 * font.GetHeight(resolution));  
  
            bitmap.Dispose();  
            bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            graphics.Dispose();  
            graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Draw the text  
            graphics.DrawString(customReportItem.Name, font, brush, font.GetHeight(resolution),   
                font.GetHeight(resolution));  
  
            // Create the byte array of the image data  
            MemoryStream memoryStream = new MemoryStream();  
            bitmap.Save(memoryStream, ImageFormat.Bmp);  
            memoryStream.Position = 0;  
            byte[] imageData = new byte[memoryStream.Length];  
            memoryStream.Read(imageData, 0, imageData.Length);  
  
            return imageData;  
        }  
    }  
}