我在Silverlight 5项目,自定义数据转换成不同颜色的IValueConverter的一个实例。 我需要从数据库中读出的实际颜色值(因为这些可以由用户编辑)。
由于Silverlight使用异步调用从数据库中加载通过实体框架的数据,我创建了一个简单的存储库,它从数据库保存的值。
接口:
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];
}
}
}
现在我想用统一到我的仓库的这种情况下注入的IValueConverter实例...
App.xaml.cs:
private void RegisterTypes()
{
_container = new UnityContainer();
IConfigurationsRepository configurationsRepository = new ConfigurationRepository();
_container.RegisterInstance<IConfigurationsRepository>(configurationsRepository);
}
的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();
}
}
问题是,我没有在转换器实例中得到相同的统一容器(即库未注册)。