使用Unity将对象注入到IValueConverter实例中

2023-11-23

我在 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];
        }
    }
}

现在我想使用 Unity 将我的存储库的这个实例注入到 IValueConverter 实例中...

应用程序.xaml.cs:

private void RegisterTypes()
{
    _container = new UnityContainer();
    IConfigurationsRepository configurationsRepository = new ConfigurationRepository();
    _container.RegisterInstance<IConfigurationsRepository>(configurationsRepository);
}

I值转换器:

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(即存储库未注册)。


可以使用MarkupExtension用于解决 DI 容器的依赖关系:

public class IocResolver : MarkupExtension
{
    public IocResolver()
    { }

    public IocResolver(string namedInstance)
    {
        NamedInstance = namedInstance;
    }

    [ConstructorArgument("namedInstance")]
    public string NamedInstance { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = (IProvideValueTarget)serviceProvider
            .GetService(typeof(IProvideValueTarget));

        // Find the type of the property we are resolving
        var targetProperty = provideValueTarget.TargetProperty as PropertyInfo;

        if (targetProperty == null)
            throw new InvalidProgramException();

        Debug.Assert(Resolve != null, "Resolve must not be null. Please initialize resolving method during application startup.");
        Debug.Assert(ResolveNamed != null, "Resolve must not be null. Please initialize resolving method during application startup.");

        // Find the implementation of the type in the container
        return NamedInstance == null
            ? (Resolve != null ? Resolve(targetProperty.PropertyType) : DependencyProperty.UnsetValue)
            : (ResolveNamed != null ? ResolveNamed(targetProperty.PropertyType, NamedInstance) : DependencyProperty.UnsetValue);
    }

    public static Func<Type, object> Resolve { get; set; }
    public static Func<Type, string, object> ResolveNamed { get; set; }
}

IocResolver 必须在应用程序启动期间初始化,如下所示:

IocResolver.Resolve = kernel.Get; 
IocResolver.ResolveNamed = kernel.GetNamed;
// or what ever your DI container looks like

之后,您可以在XAML中使用它来在XAML中注入依赖项:

<!-- Resolve an instance based on the type of property 'SomeValueToBrushConverter' -->
<MyConverter SomeValueToBrushConverter="{services:IocResolver}" />

<!-- Resolve a named instance based on the type of property 'SomeValueToBrushConverter' and the name 'MyName' -->
<MyConverter SomeValueToBrushConverter="{services:IocResolver  NamedInstance=MyName}" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用Unity将对象注入到IValueConverter实例中 的相关文章

随机推荐