question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can ViewModel have multiple instances?

See original GitHub issue

I have a “SideView” and a “SideViewModel”. I want to show it twice in “MainView”, But it can not work. It just show “DownSideView”. Code in MainView.xaml

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="1*"/>
    <RowDefinition Height="1*"/>
  </Grid.RowDefinitions>
  <Grid Grid.Row="0">
    <ContentControl x:Name="UpSideView" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  </Grid>
  <Grid Grid.Row="1">
    <ContentControl x:Name="DownSideView" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  </Grid>
</Grid>

Code in MainViewModel.cs

[Import(typeof(SideViewModel), AllowDefault = true)]
public SideViewModel UpSideView { get; set; }

[Import(typeof(SideViewModel), AllowDefault = true)]
public SideViewModel DownSideView { get; set; }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
seba30commented, Jul 22, 2019

If you decorate your ViewModel with

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]

then on every injection a new instance will be generated.

1reaction
mvermefcommented, Jul 22, 2019

The di appears to be MEF or a derivative

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple viewmodel instances : r/androiddev
Perhaps a novice question: is it a bad idea to have multiple instances of the same viewmodel type in the same scope?
Read more >
Multiple instances of android ViewModel - InsertKoinIO/koin
If ViewModel is created as a constructor parameter of another ViewModel then it can be created multiple times. ... Create a module with...
Read more >
How to avoid multiple instances of ViewModel
In MyView I've: mvvmContext1. ViewModelType = typeof(MyViewModel); While another VM instance is automatically created on View Load, so I end up ...
Read more >
Sharing same ViewModel instance between multiple Views
In our application it could be that we have multiple instances of basically the same View. Lets say we have a View with...
Read more >
WPF - How do I ensure each view gets its own ViewModel ...
In my ParentViewModel, I'm storing the instances of each ChildViewModel that is created with the user adds a new ChildUserControl to the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found