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

A shared public static variable does not change synchronously in all razor pages (in Blazor)

$
0
0

I defined a static class in order to share data among different razor pages.Here is the class:

namespace BlazorTest{    public static class General    {        public static int number;    }}

This is my parent razor:

@page "/"<button @onclick="()=>Increase()">Increase</button><br />Here is parent page.<br />@General.number<br /><Child />@code{    protected override void OnInitialized()    {        base.OnInitialized();        General.number = 0;    }    void Increase()    {        General.number++;        StateHasChanged();    }}

This is my child razor:

<button @onclick="()=>Increase()">Increase</button><br />Here is child page.<br />@General.number@code {    void Increase()    {        General.number++;        StateHasChanged();    }}

When I click the button four times in the child page, the output is:

Here is parent page.0Here is child page.4

Then I click the parent button once, it becomes:

Here is parent page.5Here is child page.4

Can I synchronize the public static class?


Viewing all articles
Browse latest Browse all 4839

Trending Articles