簡體   English   中英

IValueConverter MVVM Prism 中的依賴注入

[英]Dependency injection in 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 ,如發行說明中所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM