gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:42:13 26 4
gpt4 key购买 nike

这是我的 Caliburn.Micro 代码。

在第一步“Configure()/App.xaml.cs”中,我想将参数传递给构造函数。

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

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

public MySigletonClass (string msg){ ...

如何使用参数注册 Singletone?

更新的问题

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

在手册中,我实在无法理解“实现”。这是什么?

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 实现不支持构造函数注入(inject)以及可在解析期间提供的其他参数。还有其他依赖注入(inject)容器支持该场景,但您可以通过使用工厂模式轻松解决此限制。

以下示例演示了 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();
}
}

关于UWP,Caliburn.Micro : How to pass parameters of Constructor for Method Singleton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45788773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com