Я новичок в WPF.
Я хотел бы знать, как зависимость вводит мой класс IUnityContainer в ViewModel, который имеет код только в XAML.
Маленькое обновление:
Существует класс с именем: 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();
}
}
Если кто-то каким-то образом попадет сюда и по-прежнему будет искать ответ, это то, что я сделал в своем приложении, и все работает отлично. Ограничение класса конвертера заключается в том, что вы не можете передать ссылку на экземпляр контейнера единиц для ввода вашей службы через конструктор.
В классе 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>();