如何将ValueConverter应用于基于约定的Caliburn.Micro绑定示例?

Jap*_*Jap 5 c# xaml binding caliburn.micro windows-phone-8

我已经看到了以下问题:如何应用价值转换器到基于惯例的校准 - 微型绑定.

我无法对该主题发表评论,所以我在这里发布我的问题.

如何ConventionManager.ApplyValueConverter在使用基于约定的绑定时使用Caliburn.Micro中的值转换器?

谁能在这里写一个例子?

Ibr*_*jar 8

ApplyValueConverter被定义为类中的静态Func<>委托ConventionManager.

为了在约定绑定方案中提供自己的转换器,您需要Func<>Configure()引导程序的方法中定义自己的转换器,如下所示:

注:我假设转换是从stringOpacity.

public class AppBootstrapper : Bootstrapper<ShellViewModel> {

    private static IValueConverter StringToOpacityConverter = new StringToOpacityConverter();

    public override void Configure() {

        var oldApplyConverterFunc = ConventionManager.ApplyValueConverter;

        ConventionManager.ApplyValueConverter = (binding, bindableProperty, property) => {
            if (bindableProperty == UIElement.Opacity && typeof(string).IsAssignableFrom(property.PropertyType))
            //                                ^^^^^^^           ^^^^^^
            //                             Property in XAML     Property in view-model
                binding.Converter = StringToOpacityConverter;
                //                  ^^^^^^^^^^^^^^^^^^^^^^^^^
                //                 Our converter used here.

            // else we use the default converter
            else
                oldApplyConverterFunc(binding, bindableProperty, property);

        };
    }

}
Run Code Online (Sandbox Code Playgroud)