[DevExpress Support Team: CLONED FROM T943982: Blazor - How to integrate a custom DevExtreme component and bind it to a data source]
What if I wanted to pass the View (or, at least, the Oid of the object) to the Blazor component, if the ViewItem is added to a DetailView and not to a ListView?
Hello Emanuele,
You can access the current object in a custom View Item using the ViewItem.CurrentObject property. To access the current View, use the ViewItem.View property. You can pass these values to your Razor component when you initialize it in the CreateControlCore method. To update these values dynamically (e.g., from ViewController), you can implement a model for your Razor component. See an example at How to: Implement a Property Editor Based on a Custom Component (Blazor).
If this does not help, please describe your task in greater detail.
Hi,
here is my code:
namespace BlazorMultiAuth.Module.Blazor.Editors { /// <summary> /// /// </summary> public interface IModelDiagramViewItem : IModelViewItem { public DiagramObject DiagramObject { get; set; } public View View { get; set; } } /// <summary> /// /// </summary> [ViewItem(typeof(IModelDiagramViewItem))] public class IDiagramViewItem : ViewItem, IComplexViewItem { XafApplication application; //private object _itemObject; //private View _itemView; /// <summary> /// /// </summary> class ComponentContentHolder : IComponentContentHolder { private readonly RenderFragment componentContent; private readonly object componentObject; private readonly View componentView; public ComponentContentHolder(RenderFragment componentContent, object componentObject, View componentView) { this.componentContent = componentContent; this.componentObject = componentObject; this.componentView = componentView; } //RenderFragment IComponentContentHolder.ComponentContent => componentContent; RenderFragment IComponentContentHolder.ComponentContent => Diagram.Create(componentContent, componentObject, componentView); } public IDiagramViewItem(IModelDiagramViewItem model, Type classType) : base(classType, model.Id) { //this._itemObject = model.DiagramObject; //this._itemView = model.View; } protected override object CreateControlCore() { NavigationManager navigationManager = ((BlazorApplication)application).ServiceProvider.GetRequiredService<NavigationManager>(); Uri url = navigationManager.ToAbsoluteUri("diagram"); RenderFragment iframeFragment; iframeFragment = b => { b.OpenElement(0, "div"); b.OpenElement(1, "iframe"); b.AddAttribute(2, "src", url); b.AddAttribute(3, "width", "90%"); b.AddAttribute(4, "height", "550px"); b.CloseElement(); b.CloseElement(); }; object itemObject = this.CurrentObject; View itemView = this.View; return new ComponentContentHolder(iframeFragment, itemObject, itemView); } void IComplexViewItem.Setup(IObjectSpace objectSpace, XafApplication application) { this.application = application; } } }
And the Razor component:
@using Microsoft.JSInterop @page "/diagram" @implements IDisposable @inject IJSRuntime JSRuntime @inject DevExpress.ExpressApp.Blazor.Services.IXafApplicationProvider ApplicationProvider <h3>Diagram</h3> <div id="diagram" style="width:90%; height: 500px;"></div> @code { private DevExpress.ExpressApp.IObjectSpace objectSpace; [Parameter] public RenderFragment ComponentModel { get; set; } [Parameter] public object ComponentObject { get; set; } [Parameter] public DevExpress.ExpressApp.View ComponentView { get; set; } public static RenderFragment Create(RenderFragment componentModel, object componentObject, DevExpress.ExpressApp.View componentView) => @<Diagram ComponentModel=@componentModel ComponentObject=@componentObject ComponentView=@componentView/>; ... protected override async Task OnAfterRenderAsync(bool firstRender) { if (!firstRender) { return; } var app = ApplicationProvider.GetApplication(); objectSpace = app.CreateObjectSpace(typeof(BlazorMultiAuth.Module.BusinessObjects.DiagramObject)); ... } private DiagramUpdateInvokeHelper diagramUpdateInvokeHelper; protected override void OnInitialized() { diagramUpdateInvokeHelper = new DiagramUpdateInvokeHelper(UpdateObject); } void IDisposable.Dispose() { objectSpace?.Dispose(); } }
Anyway, this doesn't work because, while the first is sited in the Module.Blazor project, the second is sited in in the Blazor.Server project (under "Pages" folder), so the Diagram.Create() cannot be called.
Any solution? Or am I doing something wrong?
Thanks in advance.