Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnParametersSetAsync ? #15344

Closed
chanan opened this issue Dec 3, 2018 · 6 comments
Closed

OnParametersSetAsync ? #15344

chanan opened this issue Dec 3, 2018 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@chanan
Copy link

chanan commented Dec 3, 2018

Hi,

In issue aspnet/Blazor#1252 @danroth27 mentioned that OnParametersSetAsync will be added, but it doesnt seem to be there in the V0.7.0. Is it going to be added soon?

Thanks,
Chanan

@michaelvolz
Copy link

@chanan Hi, I am using it. It works as expected. Blazor 0.7

@chanan
Copy link
Author

chanan commented Dec 4, 2018

@michaelvolz What method signature are you using or what interface are you overriding?

I only see IComponent.SetParameters(ParameterCollection parameters)

@michaelvolz
Copy link

michaelvolz commented Dec 4, 2018

@chanan I simply inherit from Blazorcomponent. The IComponent interface doesn't have the methods declared.

using Microsoft.AspNetCore.Blazor.RenderTree;
using System;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Blazor.Components
{
  /// <summary>
  /// Optional base class for Blazor components. Alternatively, Blazor components may
  /// implement <see cref="T:Microsoft.AspNetCore.Blazor.Components.IComponent" /> directly.
  /// </summary>
  public abstract class BlazorComponent : IComponent, IHandleEvent, IHandleAfterRender
  {
    /// <summary>
    /// Constructs an instance of <see cref="T:Microsoft.AspNetCore.Blazor.Components.BlazorComponent" />.
    /// </summary>
    public BlazorComponent()
    {
      this._renderFragment = new RenderFragment(this.BuildRenderTree);
    }

[...]

    /// <summary>
    /// Method invoked when the component is ready to start, having received its
    /// initial parameters from its parent in the render tree.
    /// </summary>
    protected virtual void OnInit()
    {
    }

    /// <summary>
    /// Method invoked when the component is ready to start, having received its
    /// initial parameters from its parent in the render tree.
    /// 
    /// Override this method if you will perform an asynchronous operation and
    /// want the component to refresh when that operation is completed.
    /// </summary>
    /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> representing any asynchronous operation.</returns>
    protected virtual Task OnInitAsync()
    {
      return Task.CompletedTask;
    }

    /// <summary>
    /// Method invoked when the component has received parameters from its parent in
    /// the render tree, and the incoming values have been assigned to properties.
    /// </summary>
    protected virtual void OnParametersSet()
    {
    }

    /// <summary>
    /// Method invoked when the component has received parameters from its parent in
    /// the render tree, and the incoming values have been assigned to properties.
    /// </summary>
    /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> representing any asynchronous operation.</returns>
    protected virtual Task OnParametersSetAsync()
    {
      return Task.CompletedTask;
    }

    /// <summary>
    /// Method invoked after each time the component has been rendered.
    /// </summary>
    protected virtual void OnAfterRender()
    {
    }

    /// <summary>
    /// Method invoked after each time the component has been rendered. Note that the component does
    /// not automatically re-render after the completion of any returned <see cref="T:System.Threading.Tasks.Task" />, because
    /// that would cause an infinite render loop.
    /// </summary>
    /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> representing any asynchronous operation.</returns>
    protected virtual Task OnAfterRenderAsync()
    {
      return Task.CompletedTask;
    }

    /// <summary>
    /// Method invoked to apply initial or updated parameters to the component.
    /// </summary>
    /// <param name="parameters">The parameters to apply.</param>
    public virtual void SetParameters(ParameterCollection parameters)
    {
      parameters.AssignToProperties((object) this);
      if (!this._hasCalledInit)
      {
        this._hasCalledInit = true;
        this.OnInit();
        Task task = this.OnInitAsync();
        if (task != null && task.Status != TaskStatus.RanToCompletion)
          task.ContinueWith(new Action<Task>(this.ContinueAfterLifecycleTask));
      }
      this.OnParametersSet();
      Task task1 = this.OnParametersSetAsync();
      if (task1 != null && task1.Status != TaskStatus.RanToCompletion)
        task1.ContinueWith(new Action<Task>(this.ContinueAfterLifecycleTask));
      this.StateHasChanged();
    }

[...]

  }
}

@chanan
Copy link
Author

chanan commented Dec 4, 2018

Thanks @michaelvolz I will try it out!

@chanan
Copy link
Author

chanan commented Dec 4, 2018

Sadly, while it might work (not sure yet), doesn't looks like dynamic attributes will work anymore.

@SteveSandersonMS Can you suggest a solution here?

If you recall, I took your code from when you started on the bootstrap components and I am using it here: https://github.com/chanan/Blazorous/blob/master/src/Blazorous/Dynamic.cs - but when I run it as inherits from BalzorComponent so that I can use OnParametersSetAsync (which doesnt seem to exist in an interface) I no longer respects attributes as being dynamic and I get the error that I need to have the [Parameter] attribute (Or maybe I would get that error either way, not sure).

@chanan
Copy link
Author

chanan commented Dec 4, 2018

Robin Sue in the glitter.im room pointed me to https://github.com/aspnet/Blazor/issues/1659#issuecomment-437944981 which fixes my issues.

@chanan chanan closed this as completed Dec 4, 2018
@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 23, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 23, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants