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();
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();
});
}
Kindly revert to us if you still have any other queries.
I have prepared a small project which will show my problem.
I have prepared a small project which will show my problem.
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
Thank you very much.