最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Blazor base component action on creation - Stack Overflow

programmeradmin5浏览0评论

In Blazor WASM I have a service that has event property. I want to subscribe to this property immediately after the service is injected into component that is the used as a parent component(base class) for other child components.

Example:

public interface ISubService
{
    event Action<bool> OnLoad;
}

public partial class ParentComponent : ComponentBase
{

}

There are several ways how to do it but none of them do what I want (except the hackish workaround at the end)

My options are:

  1. Inject the service through [Inject] or @inject and then use OnInitialized or OnParameterSet. Theoretically I can subscribe to the event in one of those methods but the main problem I have with this is that when a child component uses OnInitialized or OnParameterSet we have to remember to call base.OnInitialized or base.OnParameterSet (Error prone + more code)

  2. The second option was to inject the service through [Inject] or @inject and then calling default constructor. This doesnt work because the constructor is called before those injected services are filled, so I cant access the event

  3. Injecting it through Constructor (.NET9 DI) but this would require us to fill the base constructor everytime it is used inside the child component.

So my main requirement is that the registration logic is only inside the parent and is not propagated in any way into child components.


I was able to achieve this but it feels like hack-ish solution. I did this:

public partial class ParentComponent : ComponentBase
{
        private ISubService? _subServiceHidden { get; set; }
        [Inject] private ISubService _subService
        {
            get => _subServiceHidden!;
            set
            {
                _subServiceHidden = value;
                _subServiceHidden.OnLoad += HandleLoadingAction;
            }
        }

    public void HandleLoadingAction(bool isLoading)
    {
        // Do something
    }
}

This way the event is registered immediately after the service is filled and I dont have to adjust child components in any way.


So my questions are:

  • Is it possible to do this in some "normal" way ? Or is this the current blazor limitation.
  • Did you need anything like this in your usecases ?

In Blazor WASM I have a service that has event property. I want to subscribe to this property immediately after the service is injected into component that is the used as a parent component(base class) for other child components.

Example:

public interface ISubService
{
    event Action<bool> OnLoad;
}

public partial class ParentComponent : ComponentBase
{

}

There are several ways how to do it but none of them do what I want (except the hackish workaround at the end)

My options are:

  1. Inject the service through [Inject] or @inject and then use OnInitialized or OnParameterSet. Theoretically I can subscribe to the event in one of those methods but the main problem I have with this is that when a child component uses OnInitialized or OnParameterSet we have to remember to call base.OnInitialized or base.OnParameterSet (Error prone + more code)

  2. The second option was to inject the service through [Inject] or @inject and then calling default constructor. This doesnt work because the constructor is called before those injected services are filled, so I cant access the event

  3. Injecting it through Constructor (.NET9 DI) but this would require us to fill the base constructor everytime it is used inside the child component.

So my main requirement is that the registration logic is only inside the parent and is not propagated in any way into child components.


I was able to achieve this but it feels like hack-ish solution. I did this:

public partial class ParentComponent : ComponentBase
{
        private ISubService? _subServiceHidden { get; set; }
        [Inject] private ISubService _subService
        {
            get => _subServiceHidden!;
            set
            {
                _subServiceHidden = value;
                _subServiceHidden.OnLoad += HandleLoadingAction;
            }
        }

    public void HandleLoadingAction(bool isLoading)
    {
        // Do something
    }
}

This way the event is registered immediately after the service is filled and I dont have to adjust child components in any way.


So my questions are:

  • Is it possible to do this in some "normal" way ? Or is this the current blazor limitation.
  • Did you need anything like this in your usecases ?
Share Improve this question edited Mar 25 at 9:48 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 20 at 13:10 KebechetKebechet 2,4572 gold badges27 silver badges43 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Do the work in SetParametersAsync.

The basic pattern to use is:

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        // Set the parameters immediately
        parameters.SetParameterProperties(this);

        //Do some sync or async work here
        // it will all complete before the UI is rendered
        await Task.Delay(TimeSpan.FromSeconds(1));

        // run the normal lifecycle methods
        // passing in an empty ParameterView.  We've already set the parameters
        await base.SetParametersAsync(ParameterView.Empty);
    }

You can see the ComponentBase code here - https://github/dotnet/aspnetcore/blob/caa429c49c4df4237d99c1c43e8b0e0faa8be9c5/src/Components/Components/src/ComponentBase.cs#L259

发布评论

评论列表(0)

  1. 暂无评论