简体   繁体   English

如何使用Prism / DryIoC在Xamarin.Forms中解决IValueConverter中的依赖关系

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

I have a Xamarin.Forms app that uses Prism and DryIoC as the container. 我有一个使用Prism和DryIoC作为容器的Xamarin.Forms应用程序。 I have a value converter where I need to make use of a service I have registered via IContainerRegistry. 我有一个值转换器,我需要使用我通过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? 如何解决这种依赖关系而不必求助于构造函数注入,因为IValueConverter是由XAML而不是DryIoC构造的? Can I use a Service Locator in Prism/DryIoC? 我可以在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();
    }
}

I would encourage you to update to the 7.1 preview as it solves this exact issue. 我鼓励您更新到7.1预览版,因为它解决了这个问题。 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: 然后您的XAML看起来像:

<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>
        <ioc:ContainerProvider x:TypeArguments="converters:MyValueConverter"
                               x:Key="myValueConverter" />
    </ContentPage.Resources>
</ContentPage>

Be sure to check out the release notes before updating though, as the release also contains some breaking changes. 请务必在更新之前查看发行说明 ,因为该版本还包含一些重大更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM