Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ContainerProviderExtension #2069

Merged
merged 8 commits into from Jul 2, 2020
63 changes: 63 additions & 0 deletions src/Wpf/Prism.Wpf/Ioc/ContainerProviderExtension.cs
@@ -0,0 +1,63 @@
using System;
using System.Windows.Markup;

namespace Prism.Ioc
{
/// <summary>
/// Provides Types and Services registered with the Container
/// </summary>
/// <example>
/// <para>
/// Usage as markup extension:
/// <![CDATA[
/// <TextBlock
/// Text="{Binding
/// Path=Foo,
/// Converter={prism:ContainerProvider {x:Type local:MyConverter}}}" />
/// ]]>
/// </para>
/// <para>
/// Usage as XML element:
/// <![CDATA[
/// <Window>
/// <Window.DataContext>
/// <prism:ContainerProvider Type="{x:Type local:MyViewModel}" />
/// </Window.DataContext>
/// </Window>
/// ]]>
/// </para>
/// </example>
dansiegel marked this conversation as resolved.
Show resolved Hide resolved
public class ContainerProviderExtension : MarkupExtension
{
public ContainerProviderExtension()
{
}

public ContainerProviderExtension(Type type)
{
Type = type;
}

/// <summary>
/// The type to Resolve
/// </summary>
public Type Type { get; set; }

/// <summary>
/// The Name used to register the type with the Container
/// </summary>
public string Name { get; set; }

/// <summary>
/// Provide resolved object from <see cref="ContainerLocator"/>
/// </summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return string.IsNullOrEmpty(Name)
? ContainerLocator.Container?.Resolve(Type)
: ContainerLocator.Container?.Resolve(Type, Name);
}
}
}