0

I have a WPF project (in .NET 4.0) with XAML resources embedded in as assembly as Pages. In the XAML, I need to have MarkupExtension that is declared in another assembly that has no specific knowledge of the assembly with the XAML.

Now, I need this MarkupExtension to be able to access the assembly in which the XAML is embedded. How is this possible?

Greg Bacchus
  • 2,235
  • 2
  • 23
  • 26

2 Answers2

1

After a bit of playing around, I worked it out:

public override object ProvideValue( IServiceProvider serviceProvider )
{
    var contextProvider = (IXamlSchemaContextProvider)serviceProvider.GetService( typeof( IXamlSchemaContextProvider ) );
    var type = contextProvider.SchemaContext.GetType().Assembly.GetType( "System.Windows.Baml2006.Baml2006SchemaContext" );
    var property = type.GetProperty( "LocalAssembly", BindingFlags.Instance | BindingFlags.NonPublic );
    var assembly = (Assembly)property.GetValue( contextProvider, null );
    ...
}

Hope that helps someone else.

Greg Bacchus
  • 2,235
  • 2
  • 23
  • 26
0

The problem is: You need the name/path of the assembly to use it in XAML. (example)

Your way is to use the MarkupExtension in the code-behind by dynamically loading your needed assembly.

seri
  • 324
  • 2
  • 11