c# - How to plug in an arbitrary view dynamically into xaml in MVVM application? -
there multiple places in application whehe have contentcontrol placed in xaml , not know beforehand content going be. best practice implement scenario?
right considering 2 approaches:
- bind
contentcontrol.contentview model , use dictionary ofdatatemplates find appropriate view. issue approach if list possible combinations in dicitonary, in cases not know exact type of view (or viewmodel if there any) @ compilation time. think, going have troubles using approach hosting non-wpf content. create sort of interface:
interface icontentplugin : idisposable { object view { get; } }and bind
contentcontrol.contenticontentplugin.viewdirectly. have multiple implementations of interface , swap them when need to. solution not strike me goes mvvm application, forces me have referencesicontentplugins in view models.
what think best option and why? perhaps there better approach?
this interesting scenario , these cases introduce viewresolverservice or viewmodelresolverservice (or both). can either give viewmodel based on view (class,type or name) match them host them in contentcontrol. or service can give view based on viewmodel (type, or string name). powerful concept can use contentcontrols and/or datatemplates , have full control.
i answered questions explaining concepts here:
register viewmodel , services in viewmodellocator
and here: get view & viewmodel plugin
more here: https://stackoverflow.com/search?q=viewmodelresolver
so if @ birds eye view need apply mvvm contentcontrols views. (and views have mvvm applied within themselves).
hth
Comments
Post a Comment