How to resolve a dependency in IValueConverter in Xamarin.Forms using Prism/DryIoC

和自甴很熟 提交于 2019-12-08 06:26:19

问题


I have a Xamarin.Forms app that uses Prism and DryIoC as the container. I have a value converter where I need to make use of a service I have registered via IContainerRegistry.

containerRegistry.RegisterSingleton<IUserService, UserService>();

How do I resolve that dependency without having to resort to constructor injection since IValueConverter gets constructed by XAML and not by DryIoC? Can I use a Service Locator in Prism/DryIoC? And if so, how?

Below is the value converter code:

public class MyValueConverter : IValueConverter
{
    private readonly IUserService _userService;

    public MyValueConverter()
    {
        // Ideally, I can use a service locator here to resolve IUserService
        //_userService = GetContainer().Resolve<IUserService>();
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isUserLoggedIn = _userService.IsLoggedIn;
        if (isUserLoggedIn)
            // Do some conversion
        else
            // Do some other conversion
        ...
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

回答1:


I would encourage you to update to the 7.1 preview as it solves this exact issue. Your converter would simply be like:

public class MyValueConverter : IValueConverter
{
    private readonly IUserService _userService;

    public MyValueConverter(IUserService userService)
    {
        _userService = userService;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isUserLoggedIn = _userService.IsLoggedIn;
        if (isUserLoggedIn)
            // Do some conversion
        else
            // Do some other conversion
        ...
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Your XAML then would look something like:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:converters="clr-namespace:DemoApp.Converters"
            xmlns:ioc="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
            x:Class="DemoApp.Views.AwesomePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <ioc:ContainerProvider x:TypeArguments="converters:MyValueConverter"
                                   x:Key="myValueConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
</ContentPage>

Be sure to check out the release notes before updating though, as the release also contains some breaking changes.



来源:https://stackoverflow.com/questions/49892472/how-to-resolve-a-dependency-in-ivalueconverter-in-xamarin-forms-using-prism-dryi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!