Issue with MVVM view first approach

2019-06-05 04:47发布

问题:

I am using mvvm architecture view first approach in my project. I mean I have view-viewmodel binding defined in resource file. But i'm unable to open multiple instances of same view...If I open the the new viewmodel will refer to the first view.

回答1:

Try setting

x:Shared="false"

Read more here



回答2:

WPF initializes data templates as singletons by default. You can override this behavior by using the x:Shared=false setting. Note: This works only with compiled resource files.

A more flexible option is to use an IoC Container for creating the associated View for a ViewModel. You might have a look at the WPF Application Framework (WAF) which shows how this works by using the Managed Extensibility Framework (MEF) as an IoC Container.



标签: wpf mvvm