UWP,Caliburn.Micro:如何为方法单例传递构造函数的参数?

UWP, Caliburn.Micro : How to pass parameters of Constructor for Method Singleton?

这是我的 Caliburn.Micro 代码。

第一步"Configure() / App.xaml.cs",我想给Constructor传递参数

_container.Singleton<MySingletonClass>("KeyName");

MySigletonClass 有一个带参数的构造函数。 (仅 1 个字符串)

public MySigletonClass (string msg){ ...

如何注册带参数的单音?

更新问题

With Caliburn.Micro manual, 注册方法有4种。有没有我们可以传递参数的类型?我应该使用 "RegisterInstance" 吗?

在手册中,我实在看不懂"implementation"。这是什么?

void RegisterInstance(Type service, string key, object implementation)
void RegisterPerRequest(Type service, string key, Type implementation)
void RegisterSingleton(Type service, string key, Type implementation)
void RegisterHandler(Type service, string key Func<SimpleContainer, object> handler)

Caliburn.Micro SimpleContainer 实现不支持使用可在解析期间提供的附加参数的构造函数注入。还有其他支持该场景的依赖注入容器,但您可以使用工厂模式轻松解决此限制。

以下示例演示了 CustomerViewModel 如何需要客户名称作为构造函数参数,但也依赖于 CustomerDatabase 实例:

/// <summary>
/// This is just some dependency that you want to use in your view models.
/// </summary>
interface ICustomerDatabase
{
    void DeleteByName(string name);
}

class CustomerDatabase : ICustomerDatabase
{
    public void DeleteByName(string text)
    {
        Console.WriteLine($"Customer '{text}' has been deleted.");
    }
}

/// <summary>
/// This customer view model has a dependency on an 
/// <see cref="ICustomerDatabase"/>-Implementation, but also 
/// needs the customer name during construction for some reason.
/// </summary>
class CustomerViewModel
{
    public CustomerViewModel(string customerName, ICustomerDatabase database)
    {
        this.Name = customerName;
        this.Database = database;
    }

    public string Name { get; }
    public ICustomerDatabase Database { get; }

    public void Delete()
    {
        this.Database.DeleteByName(this.Name);
    }
}

/// <summary>
/// To support parameters, we use the factory pattern: The factory
/// gets the view model's dependencies as its own dependencies
/// by using constructor injection. The <see cref="Create"/>-Method
/// then supplies any additional parameters. You can also use
/// multiple factory methods that correspond with different
/// constructor overloads.
/// </summary>
class CustomerViewModelFactory
{
    public CustomerViewModelFactory(ICustomerDatabase service)
    {
        this.Service = service;
    }

    public ICustomerDatabase Service { get; }

    public CustomerViewModel Create(string text)
    {
        return new CustomerViewModel(text, this.Service);
    }
}

class Program
{
    private static void Main()
    {
        var c = new SimpleContainer();

        // First, we register the factory and the service.
        c.RegisterSingleton(typeof(CustomerViewModelFactory), null, typeof(CustomerViewModelFactory));
        c.RegisterSingleton(typeof(ICustomerDatabase), null, typeof(CustomerDatabase));

        // Later on, we depend on the factory, not the view model itself.
        // We would use the factory as constructor parameter
        // everywhere we need to create view model instances.
        var factory = c.GetInstance<CustomerViewModelFactory>();

        // We create a view model instance using the factory.
        var viewModel = factory.Create("TestCustomer");

        viewModel.Delete();

        Console.ReadKey();
    }
}