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

memory state container service Blazor WebAssembly

$
0
0

I am trying to implement memory state container service in my Blazor WebAssembly project. I have 2 components (they are not coupled together as parent and child) my main component is responsible for collecting information. Attached code fragment:

@inject ServiceScopped@inject IDiposable<EditForm OnSubmit="@HandleSubmit">information collection</EditForm>@Code{  protected override async Task OnInitializedAsync()   {    ServiceScopped.Change += StateHasChanged;   }  private async Task HandleSubmit()  {    await Save();    return;  }  private async Task Save()  {   var ServiceDTO = new ServiceScoppedDTO     {     // I assign the properties entered in the form    }    ServiceScopped.UpdateData(ServiceDTO );    /* I call to open the blank window and generate the table */    await jsRuntime.InvokeVoidAsync("open", "newwindows", "_blank");  }  public void Dispose()  {   ServiceScopped.Change -= StateHasChanged;  }}

My main component stores the data but when I open the component in another tab the service appears empty:

@inject ServiceScopped@inject IDiposable// Here I print my data in an htm table@Code{    private DataDTO ServiceDTO;    protected override async Task OnInitializedAsync()    {      ServiceScopped.Change += StateHasChanged;      ServiceDTO = ServiceScopped.Data;    }    public void Dispose()    {      ServiceScopped.Change -= StateHasChanged;    }}

Program:

builder.Services.AddSingleton<ServiceScopped>();

Service:

    public class ServiceScopped{    public DataDTO Data { get; private set; } = new DataDTO();    public event Action? Change;    public void UpdateData(DataDTO newData)    {        Data = newData;        NotifyStateChanged();    }    private void NotifyStateChanged() => Change?.Invoke();}

My main component sets the data but the secondary component does not receive it, it starts empty.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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