开发者

Unity in attached properties

I need to create an attached property which communicates with some service inside it's PropertyChanged event handler. E. g.

private static void IsRegisteredPropertyChanged(DependencyObject target,
                                            DependencyPropertyChangedEventArgs e)
{
    //how to resolve this without service locator?
    IService someService = ServiceLocator.Resolve<IService>();

    if ((bool)e.NewValue)
    {
        someService.Register(target);
    }
    else
    {
        someService.Unregister(target);
    }
}

As I understand, the event handler will always be static. Is there any way to inject a dependency for this event handler using Unity开发者_如何学运维 except with Service Locator pattern? If not then maybe there are any alternatives that let you declaratively register view elements to that service?


The only way to do proper DI when the static keyword is involved is through Method Injection.

If you can inject a service through one of the method parameters, you can do it - otherwise you can't.

You can still use a Service Locator, but that's not DI...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜