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

标签 c# xamarin xamarin.forms prism dryioc

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

containerRegistry.RegisterSingleton<IUserService, UserService>();

由于 IValueConverter 是由 XAML 而不是 DryIoC 构造的,因此我如何在不必求助于构造函数注入(inject)的情况下解决该依赖关系?我可以在 Prism/DryIoC 中使用服务定位器吗?如果是这样,怎么做到的?

下面是值转换器代码:

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 预览版,因为它确实解决了这个问题。您的转换器就像:

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 将类似于:

<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>

请务必查看 release notes不过在更新之前,因为该版本还包含一些重大更改。

关于c# - 如何使用 Prism/DryIoC 解决 Xamarin.Forms 中 IValueConverter 的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49892472/

相关文章:

c# - 如何在Tizen Xamarin上播放声音

ios - Xamarin.Forms(iOS项目):ListView分隔符显示在所有屏幕中

c# - 打印队列监控 : How to get info of caller application that initiated a print job?

c# - 在 C# 中遍历 json 对象

ios - Xcode 在打开 Storyboard时崩溃

entity-framework - 跨平台共享代码的最佳实践

c# - Xamarin UISwipeGestureRecognizer 渲染器

适用于 Windows 桌面应用程序的 Xamarin 窗体?

c# - DateTime.Value.ToString(format) 给我 12 小时制

c# - 对字典对象进行排序