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:

  1. bind contentcontrol.content view model , use dictionary of datatemplates 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.
  2. create sort of interface:

    interface icontentplugin : idisposable {     object view { get; } } 

    and bind contentcontrol.content icontentplugin.view directly. have multiple implementations of interface , swap them when need to. solution not strike me goes mvvm application, forces me have references icontentplugins 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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -