c# - How to set a DataContext of multiple Views to one instance of ViewModel -




i'm using viewmodellocator views, configured in bootstrapper following method:

protected override void configureviewmodellocator() {     base.configureviewmodellocator();      viewmodellocationprovider.register<viewa, viewabviewmodel>();     viewmodellocationprovider.register<viewb, viewabviewmodel>(); } 

it works fine, makes 2 separate instances of viewmodel 2 views. want both views use same instance of viewmodel.

checking source code shows problem of creating new instance every view default:

static func<type, object> _defaultviewmodelfactory = type => activator.createinstance(type); 

prism allows define method types or special types. second case should preferred.

viewmodellocationprovider.register<viewa, viewabviewmodel>(); 

only links types of view , viewmodel together, no factory defined. means new instance created each view. use instance @ multiple views need define factory method. create 1 instance of viewmodel

viewabviewmodel vm = new viewabviewmodel(); 

and register factory methods views returning prepared instance

viewmodellocationprovider.register<viewa>(() => vm); viewmodellocationprovider.register<viewb>(() => vm); 

prism taking instance instead of creating new one.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -