IValueConverter MVVM Prism中的依赖项注入



我想在MVVM(Prism(的IValueConverter中传递依赖项注入。

我使用了ServiceLocator,但它不适用于。Net Core 3.1。

如果有任何方法可以在IValueConverter中进行依赖项注入,请告诉我。

public class RowToColorConverter : MarkupExtension, IValueConverter
{
private ICommonInventoryReference _commonInventoryReference;
/// <summary>
/// back Event Args Converter
/// </summary>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (_commonInventoryReference == null)
{
_commonInventoryReference = null;
} // CommonServiceLocator.ServiceLocator.Current.GetInstance<CommonInventoryReference>(); }
if (_commonInventoryReference == null || !(value is Inventory inventory)) { return Brushes.White; }
var brand = _commonInventoryReference.LoadBrandsByIdOrName(new BrandModel() { Name = inventory.Brand });
if (brand == null || string.Compare(brand.IsBrand, "YES", StringComparison.OrdinalIgnoreCase) == 0)
{
return Brushes.White;
}
var color = (Color)ColorConverter.ConvertFromString("#edecbb");
return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

请改用ContainerLocator,如relase注释中所示。

最新更新