我们正在将Blazor合并到我们现有的.net5.0项目中。应用程序的单独部分使用blazor。当用户进入该部分并通过单击链接更改视图时,所有用户的视图都将更改为相应的视图。
例如用户A正在查看页面A,用户B转到页面A然后单击将组件更改为页面B的链接。用户A和用户B现在是页面B。
项目位于IIS服务器上
ToCNode.razor
@using DataObjects.Help
@inject EditorState EditorState
<ul>
@foreach (var child in Children)
{
<ToCNode wikiNodeDto="@child" />
}
</ul>
@code {
[Parameter]
public WikiNodeDto wikiNodeDto { get; set; }
IEnumerable<WikiNodeDto> Children { get; set; }
string DisplayName { get; set; }
bool ShowChildren { get; set; } = false;
protected override void OnInitialized()
{
DisplayName = wikiNodeDto.Name;
Children = wikiNodeDto.Children;
base.OnInitialized();
}
void ToggleVisibility()
{
ShowChildren = !ShowChildren;
}
void SetContent()
{
EditorState.SetEditorContent(wikiNodeDto);
}
}
Content.razor
@using Components
@using DataObjects.Help
@using Models.Dbo
@inject DataManagers.Interfaces.IHelpManager HelpManager
@inject EditorState EditorState
@implements IDisposable
<div class="container body-content" id="bodyContent">
<div id="alertPlaceholder" style="margin-top:15px;"></div>
<div class="row">
<div class="col-xl-3" style="height:calc(100vh - 110px); overflow-y: auto; overflow-x: auto">
<h3 style="text-align: center; color: #17a2b8;">Table of Contents</h3>
<ToCNode wikiNodeDto="@nodes[0]" /> <!--root note-->
</div>
<div class="col-xl-9" style="height: calc(100vh - 110px); overflow-y: auto">
<Editor savedContent="@EditorState.EditorContent" OnLoadArticle="@EditorState.OnLoadArticle" />
</div>
</div>
</div>
@code {
public IList<WikiNodeDto> nodes;
public string editorContent = "";
protected override void OnInitialized()
{
nodes = HelpManager.GetAllWikiNodesDto();
EditorState.wikiNodeDto = HelpManager.GetAllWikiNodesDto()[0];
EditorState.EditorContent = EditorState.wikiNodeDto.Description;
EditorState.OnLoadArticle = true;
EditorState.OnChange += OnChangeHandler;
}
private async void OnChangeHandler()
{
await InvokeAsync(StateHasChanged);
}
public void Dispose()
{
EditorState.OnChange -= OnChangeHandler;
}
}
编辑器State.cs
using DataObjects.Help;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Components
{
public class EditorState
{
public WikiNodeDto wikiNodeDto { get; set; }
public string EditorContent { get; set; }
public bool OnLoadArticle { get; set; }
public event Action OnChange;
public void SetEditorContent(WikiNodeDto nodeDto)
{
wikiNodeDto = nodeDto;
EditorContent = nodeDto.Description;
OnLoadArticle = true;
NotifyStateChanged();
}
public void NotifyStateChanged()
{
OnChange?.Invoke();
}
}
}
启动.cs
services.AddSingleton<EditorState>(); //this was the issue
services.AddScoped<EditorState>(); // This is what it should be
编辑:更新了更多信息
第二次编辑:使用Startup.cs 更新
我猜一下:EditorState是一个单例实例。由所有用户共享。
您应该添加关于如何创建和管理它的代码。现在还不清楚它是注入的、级联的还是静态的。
根据@Henk Holterman的说法,问题是EditorState作为Singleton添加到启动中。
已将其更改为AddScoped,并且正在正常工作。