Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4045

Accessing method in a C# Blazor Dynamic Component

$
0
0

I am reasonably new to Blazor, although more experienced in C#.I am building a Proof of Concept application using Blazor and have managed to get a lot of the proof of concept completed.I am using 3 bespoke components. 2 are "normal" razor page components that get data from a database, and one is a Dynamic Component.The reason for the dynamic component is that the component to load is determined by an Id that is passed to main razor page, the Parent I guess it is.The components that are loaded dynamically are located in a Razor Component Library to make it easier to deploy a fix rather than releasing a monolith as we currently do.But the one thing I need to do is be able to call a method in the dynamic component from say, a button to keep it simple.I have spent most of the day trying to get something to work, but I have had no luck and figured there are cleaver people than me who prob. know the solution :-)

I have added the DynamicComponent tag to the razor page like this:

<DynamicComponent Type="componentType" @ref="ComponentRef" />

Then in the code behind I am doing the following:

DynamicComponent ComponentRef;protected override async Task OnParametersSetAsync()    {        Type DynamicComponentType = Type.GetType("ComponentName", true);        componentType = DynamicComponentType;    }

and this all works as I say.

but I can not figure out how to to access the dynamic component "later" when I click the button.I am using the @ref and have assigned the variable ComponentRef but I cant figure out how to asign a value for the new dynamic component to it as I am figuring that I would use this ref when I click the button. i.e. ComponentRef.TheMethodToRun();

I hope I am being clear about what I am trying to do. Any help appreciated!


Viewing all articles
Browse latest Browse all 4045

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>