【问题标题】:Pass Unity Container to a WPF Converter class将 Unity Container 传递给 WPF Converter 类
【发布时间】:2016-06-08 04:04:12
【问题描述】:

我是 WPF 新手。

我想知道如何将我的 IUnityContainer 类依赖注入到仅在 XAML 中具有代码的 ViewModel。

小更新:
有一个类名为:LiveVideoTileControl - 我已将容器添加到它。

我有一个带有特定转换器的窗口:

<UserControl x:Class="Driver.Test.Views.LiveVideoTileControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ViewModels="clr-namespace:Driver.Test.ViewModel"
         xmlns:Driver="clr-namespace:Driver.Test.DriverRelated"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" >
    <UserControl.Resources>
        <Driver:CameraToMediaElementConverter x:Key="converter"/>
    </UserControl.Resources>
    <ScrollViewer>
    <Grid>
            <ContentControl Content="{Binding  CameraEntity,Converter={StaticResource converter}}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        </ContentControl>
    </Grid>
    </ScrollViewer>
</UserControl>

如何将容器注入“CameraToMediaElementConverter”类?

class CameraToMediaElementConverter : IValueConverter
{
    public object Convert(object cameraEntity, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((cameraEntity as ICameraEntity) != null)
        {
            return DriverWrapper.GetControlForCamera((ICameraEntity)cameraEntity);
        }
        throw new NotImplementedException();
    }

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

【问题讨论】:

  • 您必须手动进行(FactoryPattern 或任何您喜欢的方式) - 这篇文章:slickthought.net/post/2011/01/17/… 展示了如何使用纯 XAML 注入属性(但仍需手动)
  • 我想我可以将我的 2 个参数包装在 1 中并将其作为对象发送到 Convert(object wrapperObj) .. 而不是在 Convert(..) 中发送 1 个对象
  • 不 - 这会让你的代码无法阅读 - 你看过我链接的文章吗?在 XAML 中为 converter 设置一个属性似乎是一个不错的选择 IMO
  • 这篇文章是在讨论将其注入到 ViewModel,而不是注入到我在视图模型中使用的静态转换器类。
  • 这种情况下的区别在哪里?您将一个声明为 DataContext,另一个声明为 Resource

标签: c# wpf converter unity-container


【解决方案1】:

如果有人会以某种方式到达这里并且仍然会寻找答案,这就是我在我的应用程序中所做的并且它工作得很好。转换器类的限制是您不能通过构造函数传递对统一容器实例的引用来注入您的服务。

在 Bootstrapper 类或您在启动时注册服务的任何其他地方(这里它是 Prism 的引导程序 ConfigureContainer 的一部分):

   protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<IShellViewModel, ShellViewModel>();
        Container.RegisterType<MyService>(new ContainerControlledLifetimeManager());
        Container.RegisterInstance<MyService>(new MyService());
        Application.Current.Resources.Add("IoC", this.Container);
    }

注意最后一行:

            Application.Current.Resources.Add("IoC", this.Container);

在转换器类ctor中:

        UnityContainer unityContainer = (UnityContainer)Application.Current.Resources["IoC"];

现在您可以轻松地从 Unity 容器中解析对象的任何实例:

service = (MyService) unityContainer.Resolve<MyService>();

【讨论】:

  • 感谢这是一个很好的解决方案,但是在以这种方式解析 UnityContainer 时我不得不更改一些代码:UnityContainerExtension unityContainer = (UnityContainerExtension)Application.Current.Resources["IoC"]; 解析服务时:var service = (IMyService) unityContainer.Resolve(typeof(IMyService));
猜你喜欢
  • 2010-11-10
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
相关资源
最近更新 更多