Creating SfPdfViewerServer by RenderFragment and getting reference by AddComponentReferenceCapture

  public SfPdfViewerServer PdfViewer { get; set; }  // reference for pdf viewer

  protected RenderFragment RenderComponentHandler { get; set; } // renderer

protected RenderFragment CreatePdfComponent() => builder => 

 {

 builder.OpenComponent(0);

 builder.AddComponentReferenceCapture(1, (inst) => { PdfViewer = inst as SfPdfViewerServer; }); // capturing reference here

 builder.AddAttribute(2, "Height", "590px"); 

 builder.AddAttribute(3, "DocumentPath", pdfPath); 

 builder.CloseComponent(); 

 };

after  running the code below ,  PdfViewer  is always null. I could not understand why ?

 RenderComponentHandler = CreatePdfComponent();




6 Replies 1 reply marked as answer

VV Visvesvar Venkatesan Syncfusion Team November 25, 2022 03:31 PM UTC

We have provided a code snippet and sample for getting a reference in component reference capture.


Code snippet:


private SfPdfViewerServer _viewer = null;

    private string pdfPath { get; set; } = "wwwroot/PDF_Succinctly.pdf";


    public void Onclick()

    {

       

    }

    RenderFragment DrawCounter()

    {

        return new RenderFragment(builder =>

        {

            builder.OpenComponent(0, typeof(SfPdfViewerServer));

            builder.AddAttribute(1, "Height", "590px");

            builder.AddAttribute(2, "DocumentPath", pdfPath);

            builder.AddComponentReferenceCapture(3, inst => { _viewer = (SfPdfViewerServer)inst; });

            builder.CloseComponent();

        });

    }



Sample - https://www.syncfusion.com/downloads/support/directtrac/general/ze/componentreferencecapture-1008192453.zip


Kindly revert to us if you still have any other queries.



EM Emre Misirlioglu November 26, 2022 09:32 AM UTC

I have prepared a small project which will show my problem. 




EM Emre Misirlioglu November 26, 2022 09:33 AM UTC

I have prepared a small project which will show my problem. 




EM Emre Misirlioglu replied to Visvesvar Venkatesan November 26, 2022 09:39 AM UTC

 have prepared a small project which will show my problem.


Attachment: QMS_139ba2a7.rar


VV Visvesvar Venkatesan Syncfusion Team November 28, 2022 09:42 AM UTC

From your sample, we thought that you have try to set the DocumentPath after creating the component. Your component instance will be set after the component is rendered on the UI. So please use the created event to set the DocumentPath.


    public void OnCreated()

    {

        _viewer.DocumentPath = pdfPath;

    }


    RenderFragment DrawCounter()

    {

        return new RenderFragment(builder =>

        {

            builder.OpenComponent(0, typeof(SfPdfViewerServer));

            builder.AddAttribute(1, "Height", "590px");

            //builder.AddAttribute(2, "DocumentPath", pdfPath);

            builder.AddAttribute(3, "ChildContent", (RenderFragment)((builder2) =>

                   {

                       builder2.OpenComponent(4, typeof(PdfViewerEvents));

                       builder2.AddAttribute(5, "Created", Microsoft.AspNetCore.Components.EventCallback.Factory.Create<object>(this, OnCreated));

                       builder2.CloseComponent();

                   }));

            builder.AddComponentReferenceCapture(6, inst => { _viewer = (SfPdfViewerServer)inst; });

            builder.CloseComponent(); 

        });

    }


Please find the sample also: https://www.syncfusion.com/downloads/support/directtrac/general/ze/179045~1-1709857762.zip


Marked as answer

EM Emre Misirlioglu replied to Visvesvar Venkatesan November 28, 2022 12:13 PM UTC

Thank you very much.


Loader.
Up arrow icon