简体   繁体   English

为所有用户更改 Blazor 组件

[英]Blazor Component changing for all users

we are incorporating Blazor into our existing .net5.0 project.我们正在将 Blazor 合并到我们现有的 .net5.0 项目中。 Separate section of the application is using blazor.应用程序的单独部分正在使用 blazor。 When a user enters that section and changes the view by clicking a link, the view for all users will change to that corresponding view.当用户进入该部分并通过单击链接更改视图时,所有用户的视图都将更改为相应的视图。

eg User A is viewing pageA, User B goes to page A and then clicks a link that would change the component to pageB.例如,用户 A 正在查看页面 A,用户 B 转到页面 A,然后单击将组件更改为页面 B 的链接。 User A and User B are now pageB.用户 A 和用户 B 现在是 pageB。

Project is on an IIS Server项目在 IIS 服务器上

ToCNode.razor 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内容.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;
    }
}

EditorState.cs编辑状态.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();
        }
    }
}

Startup.cs启动文件

services.AddSingleton<EditorState>(); //this was the issue
services.AddScoped<EditorState>(); // This is what it should be

Edit: Updated with more information编辑:更新了更多信息

2nd Edit: Updated with Startup.cs第二次编辑:使用 Startup.cs 更新

I'll take a guess: EditorState is a singleton instance.我猜猜:EditorState 是一个单例实例。 Shared by all users.由所有用户共享。

You should add the code for how it is created and managed.您应该添加有关如何创建和管理它的代码。 It is now not clear if it is injected or cascaded or just static.现在还不清楚它是注入的还是级联的,还是只是静态的。

As per @Henk Holterman, issue was that the EditorState was added to the startup as a Singleton.根据@Henk Holterman,问题是 EditorState 作为单例添加到启动中。

Changed it to AddScoped and is working as it should.将其更改为 AddScoped 并正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM