BlendabilityBarry Gervinbgervin@objectsharp.comObjectSharp, Founding Partnerwww.ObjectSharp.com/blogs/barry
We should...
But sometimes we...
We Should...
But sometimes we...
What is “Blendability”?“Ability to work on a project in Expression Blend”“Developers and Designers can work within their own tools, in harmony with each other,  throughout the development lifecycle”“Valuing the declaration of intention in our code 			overthe fine grain control of proceduraloperations.”
does it blend?
Not Blendable!
It Blends!
Blendability
Demo: Blend ToolingDesign Time Attributes, Sample Data, Visual State Manager
XML Data SourceAttractive option for designers, but...Generates unique syntaxDifficult to move to CLR object binding.
PracticesTechniquesData BindingModel Concerns (Change Notification)MVVMValue ConvertersControl TemplatesTriggers, Actions & BehavioursSource Code Management
Model ConcernspublicstringViewModelProperty { get{ returnthis.viewModelProperty; } set{ this.viewModelProperty = value; this.NotifyPropertyChanged("ViewModelProperty"); } }
Model ConcernspublicclassItemCollection : ObservableCollection<Item> { .. }...privateItemCollection _myCollection =newItemCollection(); //make it blend!publicItemCollectionMyCollection{ get	{ returnthis._myCollection; 	} }
Model-View-ViewModelPattern created by John Gossman from WPF teamInspired by MVC, MVP & Martin Fowler’s PresentationModel patternExcellent choice for BindabilityLots of evolution, popularity, frameworks
Why MVVM?Separation of Concerns (UI & Business Logic)Event driven code is not very unit testableViews & View Models don’t have to be changed by changes in each other.Blendability
MVVM.DataContextViewViewModelModel{Binding .. }WindowUser ControlPOCO,
EF/L2S Entity
DTO,
WCF ProxyDomain / Service / Repository
With Model-View-PresenterViewModel /Business LayerPresenterSelectionSave
With Model-View-ViewModelViewModel / Business LayerViewModel{Binding}Selection{Binding}Save{Binding}
ViewModel ConstructionpublicpartialclassMainView : UserControl{ publicMainView() { this.DataContext = newMyViewModel(); InitializeComponent(); } }Not very blendable, neither is DI.
ViewModel Construction<UserControl.Resources><local:MainViewModelx:Key="MainViewModelDataSource"/></UserControl.Resources><Grid x:Name="LayoutRoot"DataContext="{Binding Source={StaticResourceMainViewModelDataSource}}" d:DataContext="{d:DesignData /SampleData/MainViewModelSampleData.xaml}">Nice!
MVVM DemoInvoking Actions, Update the UI
ViewModel InstantiationView ConstructorDependency InjectionXAML (Easiest Blendability)ViewModelLocator Service
MVVM FrameworksMVVM Toolkit
Composite WPF (Prism)
MVVM Foundation
MVVM Light Toolkit
OnyxDefined as part of a binding definition:<TextBox.Background>    <Binding Path=“AccountBalance">        <Binding.Converter>            <local:BalanceToBrushConverter/>        </Binding.Converter>    </Binding></TextBox.Background>Blendable, but do you always want to do this?Value Converter in XAML
Value Converter vs. View ModelFull Name = LastName + “, “ + FirstNameBetter candidate on the Model or ViewModelAsk yourself...How reusable is this? Will I need to test this as part of the test for the View Model?
Control TemplatesExpressing Intention
Interactivity without CodeSilverlight 2Storyboards  with Animations, triggered playbackInteractive Controls Data BindingsData Templates...and code, frequently common code
Interactivity without CodeSilverlight 3 & 4Triggers & Actions (a la WPF)Action = Object that is invoked to perform operation Change PropertyOpen WindowTrigger = Event that causes some behaviourClickedTimer
Interactivity without CodeSilverlight 3 & 4BehavioursSelf-ContainedReusable“snippets” of interactivity code, Trigger + Action?Assigned to art board contentWorks in BlendExcellent for Prototyping AND Production
BehavioursInherit from Behaviour<T>OverrideOnAttached<T> AssociatedObjectprogrammatically add xaml, storyboards, etc.Assign event handlersImplement Event Handlers (i.e. Triggers)Begin StoryboardSet Propertys, etc.
BehavioursAdd interactivity without code.
Triggers, Actions & BehavioursTrigger: Event that causes a behaviourAction: Behaviour that is invoked on some trigger.Behaviour: Much more, but think Trigger + Action
Custom BehaviourpublicclassInverseColorClickBehavior :Behavior<FrameworkElement>{ protectedoverridevoidOnAttached(){ base.OnAttached(); this.AssociatedObject.MouseLeftButtonDown += newMouseButtonEventHandler(  AssociatedObject_MouseLeftButtonDown );}privatevoidAssociatedObject_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ){ this.AssociatedObject.Effect = this.AssociatedObject.Effect == null ?  	this.AssociatedObject.Effect = this.inverseColor : 	this.AssociatedObject.Effect = null;}}Determines type of AssociatedObject, and what you can do?Event handlers, customize element.Don’t forget to clean up in Detached!

Will your code blend? : Toronto Code Camp 2010 : Barry Gervin

Editor's Notes

  • #35 Category Support in Asset LibraryWe can support Categories in Asset Library using a “MetadataStore”IconsWe can assign icons by setting a PNG image as Embedded Resource and naming in a specific format:Namespace.ControlName.[Expression].[small|medium|etc].png“Installing” BehaviorsCOPY TO %ProgramFiles%Microsoft ExpressionBlend 3LibrariesSilverlightv3.0