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

Adding a singleton in a blazor Web Assembly project

$
0
0

I am currently learning Blazor Web assembly. So I have created a Blazor Web Assembly sandbox on .Net 6. I want to create an object that can't be reloaded on the entire lifecycle of the app, in order to share it between pages. So, I want to create it as singleton. But I have this error.

enter image description here

Here is the code of Program.cs :

using BlazorAppWebAsmSandbox.Client;using Microsoft.AspNetCore.Components.WebAssembly.Hosting;var builder =WebAssemblyHostBuilder.CreateDefault(args);builder.Services.AddSingleton<GuidObject>();await builder.Build().RunAsync();

GuidObject :

namespace BlazorAppWebAsmSandbox.Client{    public class GuidObject    {        public Guid id { get; set; } = Guid.NewGuid();    }}

And here how I use it :

@page "/counter"@rendermode InteractiveWebAssembly@inject GuidObject guidObject<PageTitle>Counter</PageTitle><h1>Counter</h1><p role="status">Current count: @currentCount</p><p role="status">guidObject @guidObject.id</p><button class="btn btn-primary" @onclick="IncrementCount">Click me</button>@code {    private int currentCount = 0;    private void IncrementCount()    {        currentCount++;    }}

Thanks for your help


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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