| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

MVVM And Blend

Page history last edited by Andy Sherwood 14 years, 2 months ago

MVVM And Blend?

 

Why we need design-time support MVVM

Designers need it to see data - "an empty list box doesn't help". It becomes really important when you're talking about styling UI based on the data it's representing. This is especially important at the beginning of the development cycle. However, maintaining two models, one for design-time sample data and one for run-time can be painful.

 

The role of behaviors

Developers can create attached behaviors to provide functionality for designers. Behaviors run a chunk of code on the element they're attached to when the element is instantiated. Behaviors are reusable and easy for designers to apply. However, they are difficult to test.

 

The role of containers

There's a bit of an open question as to whether or not IoC containers help or hinder.

Do you want IoC at design time?

Container to load data at design time?

Depending on IoC beings in EVERYTHING

 

Possible solutions

 

Just cram it in there

You can use the d:DataContext attribute to attach a VM directly to a View. The trouble with this approach is you need drag EVERYTHING into Blend (repositories, services, etc.)

 

Use a custom ViewModel binder

Glenn has put together a ViewModel binder that supports Blend. It uses an ApplicationResource and controls derived from ViewModelControl to expose the ViewModel to Blend. It's possible to support ItemsControls as well.

 

Use an assembly attribute to activate design-time behavior

Josh Smith has a clever hack to get design-time behavior in Blend. He uses an assembly attribute, which, when evaluated determines if the assembly is loaded under a designer and changes behavior.

 

Use a screen conductor and IoC to swap out different container profiles

If you avoid constructor injection, you can use a screen conductor and attribute injection to push the correct data into the controls. Use the IoC container to provide different profiles for run time vs. design time. The design time profile would provide stubs exposing sample data. Common problems with IoC containers (i.e. missing dependencies) can be debugged using container diagnostics tools.

 

Use and event aggregator to bubble up events

Jeremy proposed using an event aggregator to bubble up events through messaging and a pub-sub system.

 

Miscellaneous

+10 Points to Glenn - Projector Fail Achievement Unlocked!

Bindings suck - magic strings

Control names are bad

VisualStateManager comes in the box of WPF4, available as an addin to WPF3.

Blend has better support for styles

Blend 4 has much better support for MVVM - but more is needed.

Want to intercept user control creation in Blend

Application.Resources will run any code you throw at it

Conventions only work with a social contract

 

 

Comments (0)

You don't have permission to comment on this page.