Welcome to WindowsClient.net | Sign in | Join

Rob Relyea - XAMLified

WPF, Silverlight and XAML

Syndication

Sponsors





  • advertise here
Inversion of Control (IOC) with XAML

I met Simon Ferquel this week at the 2010 MVP Summit. I gave a talk about XAML on Wednesday, and later that day blogged one of my samples from the talk: “XamlSchemaContext/XamlType/XamlMember – a command line example”.

He commented on that post with:

Hi Rob,

As I understand, creating a custom XamlSchemaContext could be a way to make Xaml content integrate with an IoC, isn't it?

I can imagine a scenario where you can then write something like :

<Foo>

<Foo.Bar><IBar /></Foo.Bar>

</Foo>

With IBar being an interface that is resolved by the IoC. Same thing could apply with types without default constructor whose dependencies could be injected by the IoC-enabled schema context.

If you confirm that it is doable, I'll make a sample of a Unity-enabled Xaml Schema Context.

by Simon Ferquel

Turns out that subclassing XamlSchemaContext and XamlType seems to have worked quite nicely. See Simon’s “[Xaml] IoC-enabled Xaml parser” The basic idea is the custom schema context returns a XamlType for IBar that says it knows how to be created.  When the XamlObjectWriter calls it to create the IBar, it can find an appropriate IBar to return.

Other Approaches

Other people have accomplished Invesrion of Control other ways…

  • John “Z-Bo” Zabroski uses a custom markup extension. (via comment)
  • I’ll add more links as I find other approaches or comments mention them.
Published Sunday, February 21, 2010 2:55 PM by Rob_Relyea
Filed under: ,

Comments

# Dew Drop February 21-22, 2010 | Alvin Ashcraft&#039;s Morning Dew@ Sunday, February 21, 2010 8:58 PM

Pingback from  Dew Drop February 21-22, 2010 | Alvin Ashcraft&#039;s Morning Dew

# re: Inversion of Control (IOC) with XAML@ Monday, February 22, 2010 2:29 AM

Hi Rob, thanks a lot for your post!

For this thing to be completely usable in a WPF world, it would be nice to be able to tell the xaml designer / baml compiler that a particular invalid element will be resolved at runtime, and that it should not break the design-time experience or the baml compilation process.

For now it works only with xaml-only wpf. The best approach for real-life WPF scenarios seems to be the usage of Markup Extensions.

by Simon

# Daily tech links for .net and related technologies - Feb 22-24, 2010@ Monday, February 22, 2010 5:03 AM

Daily tech links for .net and related technologies - Feb 22-24, 2010 Web Development Partial Validation

# re: Inversion of Control (IOC) with XAML@ Monday, February 22, 2010 5:46 AM

Simon-

Yes, the runtime XAML engine enables a number of scenarios, but it is difficult to communicate these flexibilities to designers and compilers.

Clearly, enabling this flexibility without having to throw away compilation would be ideal.

As you have realized, you can use a MarkupExtension (whose goal is to create the IBar) and still keep the XAML Compiler and Designers happy.

Thanks, Rob

# Linked -> List@ Monday, February 22, 2010 2:08 PM

Linked -> List

# Markus Tamm &raquo; Blog Archive &raquo; Links 23.02.2010@ Tuesday, February 23, 2010 4:19 AM

Pingback from  Markus Tamm  &raquo; Blog Archive   &raquo; Links 23.02.2010

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Page view counter