设置范围弹出表单 属性 数据

Set scoped popup form property data

我有一个 winforms 应用程序,它根据运行时数据在运行时动态创建子表单弹出窗口。我会 就像每个弹出窗口都有自己的范围一样,我认为我已经实现了。另外,我想根据运行时数据设置一个由范围解析的“玩家配置文件”。这是我需要帮助的地方。

每个弹出表单都有一个配置文件数据属性:

public partial class PlayerForm : Form
{
    public IPlayerProfile Profile { get; set; }
}

我使用注册表格生成器打开表格:

public TForm GetForm<TForm>(ActiveGame gameInfo) where TForm : PlayerForm
{
    var scope = new Scope(_container);
    var form = scope.GetInstance<TForm>();
    form.GameInfo = gameInfo;

    // set the profile:
    // form.Profile = ...

    form.Closed += (s, e) => scope.Dispose();
    return form;
}

scope 没有 GetAllInstances 方法。如何在此处获取和设置匹配的 IPlayerProfilegameInfo 包含用于查找适当配置文件的类型名称。

相关容器配置:

container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;

var registrations = profiles.Select(t => Lifestyle.Scoped.CreateRegistration(t, container));
container.Collection.Register<IPlayerProfile>(registrations);

调用 Container.GetAllInstances<T>() 等同于 Container.GetInstance<IEnumerable<T>>(),这也适用于 Scope,正如您提到的,缺少 GetAllInstances 方法。所以你可以这样称呼:

var profiles = scope.GetInstance<IEnumerable<IPlayerProfile>>();

或者您可以解析任何其他 supported collection type,例如:

var profiles = scope.GetInstance<IReadOnlyCollection<IPlayerProfile>>();