本文介绍了如何使用 Prism/DryIoC 解决 Xamarin.Forms 中 IValueConverter 中的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xamarin.Forms 应用程序,它使用 Prism 和 DryIoC 作为容器.我有一个值转换器,我需要在其中使用我通过 IContainerRegistry 注册的服务.

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>();

由于 IValueConverter 是由 XAML 而不是 DryIoC 构造的,我该如何解决该依赖关系而不必求助于构造函数注入?我可以在 Prism/DryIoC 中使用服务定位器吗?如果是这样,如何?

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?

以下是值转换器代码:

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();
    }
}

推荐答案

我鼓励您更新到 7.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();
    }
}

您的 XAML 将类似于:

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.

这篇关于如何使用 Prism/DryIoC 解决 Xamarin.Forms 中 IValueConverter 中的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:40