如何在XAML中注入转换器

时间:2022-07-12 14:14:00

I have an IValueConverter implemented class and I need it to be injected using my DI container (Ninject).

我有一个IValueConverter实现的类,我需要用我的DI容器(Ninject)注入它。

The problem is, in XAML, there's no immediately obvious way to get control over the instantiation of the Converter object.

问题是,在XAML中,没有立即明显的方法来控制转换器对象的实例化。

So my XAML contains a line something like this:

XAML中有这样一行

Source="{Binding Path=CurrentMessage, Converter={StaticResource ImagePathConverter}}"

源= " {绑定路径= CurrentMessage转换器= { StaticResource ImagePathConverter } }”

Where, the ImagePathConverter will be created for me.

其中,ImagePathConverter将为我创建。

I suppose I could create a "service locator" static class and call it to resolve my dependency and change the StaticResource to a property "MyServiceLocator.TheImageConverter", but that makes me want to vomit.

我假设我可以创建一个“服务定位器”静态类,并调用它来解决我的依赖关系,并将StaticResource更改为一个属性“MyServiceLocator”。但是这让我想吐。

I am hoping this question can be answered with a few snippets of code that specifically target the code supplied - and perhaps a supporting link to an example. Not simply a recommendation to take a look somewhere.

我希望可以用一些特定于所提供代码的代码片段来回答这个问题,也许还可以提供一个示例的支持链接。不仅仅是建议你去找个地方看看。

Also, very importantly, assume that the XAML does not have a code-behind - and that I cannot use one. I'm creating a skin and do not want a code behind. So I cannot set a class variable in the class constructor and reference it. Maybe that's unreasonable, I'm not sure yet.

同样,非常重要的是,假设XAML没有代码隐藏——而且我不能使用代码隐藏。我正在创建一个皮肤,不希望后面有代码。所以我不能在类构造函数中设置类变量并引用它。也许这是不合理的,我还不确定。

2 个解决方案

#1


8  

A common way to handle this is for your converter to also be a MarkupExtension. That is:

处理此问题的一种常见方法是将转换器也作为MarkupExtension。那就是:

public class MyConverter : MarkupExtension, IValueConverter

Your ProvideValue() method can return an instance of your converter, thus allowing you to use it like this:

您的ProvideValue()方法可以返回转换器的实例,因此允许您这样使用它:

Source="{Binding CurrentMessage, Converter={local:MyConverter SomeParameterToConverter}}"

This isn't really anything to do with DI, but it does address your requirement to eliminate the code behind. I don't really see the point of having converters registered with your DI container.

这实际上与DI没有任何关系,但它确实满足了您消除背后代码的需求。我不认为让转换器注册到DI容器中有什么意义。

#2


0  

An alternative approach is, to resolve the dependency via MarkupExtension and set it to the converter's property in XAML.

另一种方法是,通过MarkupExtension解决依赖关系,并将其设置为XAML中的转换器属性。

See the following answer for details:

详情请参阅以下答案:

https://*.com/a/41611854/2115905

https://*.com/a/41611854/2115905

#1


8  

A common way to handle this is for your converter to also be a MarkupExtension. That is:

处理此问题的一种常见方法是将转换器也作为MarkupExtension。那就是:

public class MyConverter : MarkupExtension, IValueConverter

Your ProvideValue() method can return an instance of your converter, thus allowing you to use it like this:

您的ProvideValue()方法可以返回转换器的实例,因此允许您这样使用它:

Source="{Binding CurrentMessage, Converter={local:MyConverter SomeParameterToConverter}}"

This isn't really anything to do with DI, but it does address your requirement to eliminate the code behind. I don't really see the point of having converters registered with your DI container.

这实际上与DI没有任何关系,但它确实满足了您消除背后代码的需求。我不认为让转换器注册到DI容器中有什么意义。

#2


0  

An alternative approach is, to resolve the dependency via MarkupExtension and set it to the converter's property in XAML.

另一种方法是,通过MarkupExtension解决依赖关系,并将其设置为XAML中的转换器属性。

See the following answer for details:

详情请参阅以下答案:

https://*.com/a/41611854/2115905

https://*.com/a/41611854/2115905