使用Unity将对象注入IValueConverter实例 [英] Using Unity to inject objects into IValueConverter instance
问题描述
我在Silverlight 5项目中有一个IValueConverter实例,该实例将自定义数据转换为不同的颜色.我需要从数据库中读取实际的颜色值(因为这些值可以由用户编辑).
I have an instance of IValueConverter in a Silverlight 5 project, which converts custom data into different colors. I need to read the actual color values from a database (as these can be edited by the user).
由于Silverlight使用异步调用从数据库中通过Entity Framework加载数据,因此我创建了一个简单的存储库,用于保存db中的值.
Since Silverlight uses asynchronous calls to load the data through Entity Framework from the database, I created a simple repository, which holds the values from the db.
界面:
public interface IConfigurationsRepository
{
string this[string key] { get; }
}
实现:
public class ConfigurationRepository : IConfigurationsRepository
{
private readonly TdTerminalService _service = new TdTerminalService();
public ConfigurationRepository()
{
ConfigurationParameters = new Dictionary<string, string>();
_service.LoadConfigurations().Completed += (s, e) =>
{
var loadOperation = (LoadOperation<Configuration>) s;
foreach (Configuration configuration in loadOperation.Entities)
{
ConfigurationParameters[configuration.ParameterKey] = configuration.ParameterValue;
}
};
}
private IDictionary<string, string> ConfigurationParameters { get; set; }
public string this[string key]
{
get
{
return ConfigurationParameters[key];
}
}
}
现在,我想使用Unity将我的存储库实例注入IValueConverter实例...
Now I would like to use Unity to inject this instance of my repository into the IValueConverter instance...
App.xaml.cs:
App.xaml.cs:
private void RegisterTypes()
{
_container = new UnityContainer();
IConfigurationsRepository configurationsRepository = new ConfigurationRepository();
_container.RegisterInstance<IConfigurationsRepository>(configurationsRepository);
}
IValueConverter:
IValueConverter:
public class SomeValueToBrushConverter : IValueConverter
{
[Dependency]
private ConfigurationRepository ConfigurationRepository { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((SomeValue)value)
{
case SomeValue.Occupied:
return new SolidColorBrush(ConfigurationRepository[OccupiedColor]);
default:
throw new ArgumentException();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
问题是,我在转换器实例中没有获得相同的Unity-Container(即,存储库未注册).
The problem is, that I do not get the same Unity-Container in the converter instance (ie. the repository is not registered).
推荐答案
根据您的评论,您需要使用ServiceLocator来获取ConfigurationRepository的实例,因为Converter的实例不是由Unity创建的,但是由Silverlight/XAML引擎提供.
According to your comment, you need to use a ServiceLocator to get an instance of your ConfigurationRepository, cause the instance of the Converter isn´t created by Unity, but by the Silverlight/XAML engine.
因此,不会注入使用DependencyAttribute装饰的属性.
So your property that is decorated with the DependencyAttribute won´t be injected.
c#
public class SomeValueToBrushConverter : IValueConverter
{
public SomeValueToBrushConverter(){
ConfigurationRepository = ServiceLocator.Current.GetInstance<ConfigurationRepository>();
}
private ConfigurationRepository ConfigurationRepository { get; set; }
}
在您的RegisterTypes方法中,您需要配置ServiceLocator:
In your RegisterTypes method you need to configure the ServiceLocator:
_container = new UnityContainer();
UnityServiceLocator locator = new UnityServiceLocator(_container);
ServiceLocator.SetLocatorProvider(() => locator);
这篇关于使用Unity将对象注入IValueConverter实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!