Blazor:如何使用来自具有 2 个不同状态的 2 个不同页面的组件

Blazor: How to use a component from 2 different pages with 2 different states

在 Blazor 应用程序中,我想使用来自两个不同页面的组件。该组件使用服务来维护其状态。但是每个页面都需要让组件使用不同的状态。以下是我的想法(使用默认的 Counter 组件进行演示)。

CounterService.cs

namespace TestTwoInterfaceToOneService.Shared
{
    public interface ICounterServiceA
    {
        int CurrentCount { get; set; }
        void IncrementCount();
    }

    public interface ICounterServiceB
    {
        int CurrentCount { get; set; }
        void IncrementCount();
    }

    public class CounterService : ICounterServiceA, ICounterServiceB
    {
        public int CurrentCount { get; set; }
        public void IncrementCount()
        {
            CurrentCount++;
        }
    }
}

在Program.cs中添加:

builder.Services.AddScoped<ICounterServiceA, CounterService>();
builder.Services.AddScoped<ICounterServiceB, CounterService>();

Counter.razor

<h1>Counter</h1>
<p>Current count: @CounterService.CurrentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    [Parameter]
    public CounterService CounterService { get; set; }

    private void IncrementCount()
    {
        CounterService.CurrentCount++;
    }
}

PageA.razor

@page "/pagea"
@inject ICounterServiceA CounterServiceA

<h3>Page A</h3>
<Counter CounterService="CounterServiceA" /> @*<-- Error: Cannot convert from ICounterServiceB to CounterService*@

PageB.razor

@page "/pageb"
@inject ICounterServiceB CounterServiceB

<h3>Page B</h3>
<Counter CounterService="CounterServiceB" /> @*<-- Error: Cannot convert from ICounterServiceB to CounterService*@

当我尝试将服务传递给组件时出现错误 'Cannot convert from ICounterServiceB to CounterService'。我发现使用 2 个相同的接口指向相同的具体实现确实给了我 2 个作用域实例。但是,我不知道如何将这些实例传递到组件中。

有没有我遗漏的部分?或者,这应该以其他方式完成吗?

解决方案

结合使用 Henk 的回答和 Brian 的评论,我得出的解决方案是:

CounterService.cs

namespace TestTwoInterfaceToOneService.Shared
{
    public class CounterService
    {
        public int CurrentCount { get; set; }
        public void IncrementCount()
        {
            CurrentCount++;
        }
        public void ResetCount()
        {
            CurrentCount = 0;
        }
    }
}

CounterStateService.cs

using System.Collections.Generic;
namespace TestTwoInterfaceToOneService.Shared
{
    public interface ICounterStateService
    {
        CounterService this[string key] { get; }
    }

    public class CounterStateService : ICounterStateService
    {
        private Dictionary<string, CounterService> counterServices = new();
        public CounterService this[string key]
        {
            get
            {
                if (!counterServices.ContainsKey(key))
                {
                    counterServices.Add(key, new CounterService());
                }
                return counterServices[key];
            }
        }
    }
}

在Program.cs中添加

    builder.Services.AddScoped<ICounterStateService, CounterStateService>();

Counter.razor

<p>Current count: @CounterService.CurrentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>

@code {
    [Parameter]
    public CounterService CounterService { get; set; }

    private void IncrementCount()
    {
        CounterService.CurrentCount++;
    }
}

PageA.razor

@page "/pagea"
@inject ICounterStateService CounterStateService

<h3>Page A</h3>
<Counter CounterService='CounterStateService["A"]' />
<button class="btn btn-primary" @onclick='CounterStateService["A"].ResetCount'>Reset Count</button>

PageB.razor

@page "/pageb"
@inject ICounterStateService CounterStateService

<h3>Page B</h3>
<Counter CounterService='CounterStateService["B"]' />
<button class="btn btn-primary" @onclick='CounterStateService["B"].ResetCount'>Reset Count</button>

Or, should this be done some other way?

是的。将这种使用模式融入类型结构不会很好地适应或扩展。如果您需要第三页,或者想让它动态化怎么办?

您可以使用包装器服务和带有键的方案将状态绑定到组件:

public class CounterService  { ... }    // no need to register

public class CounterStateService  // register for injection
{

    private Dictionary <string, CounterService> stateLookup = new();

    public CounterService  this[string key] 
    {
      if (! stateLookup.tryGetValue (key, out CounterService  service))
      {
         service = new CounterService();
         stateLookup.Add(key, service);
      }
      return service;
    }
}

并像

一样使用它
@page "/pagea"
@inject CounterStateService  CounterStateService

<h3>Page A</h3>
<Counter CounterService="CounterStateService["A"]" />