Can ViewModel have multiple instances?
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you decorate your ViewModel with
then on every injection a new instance will be generated.
The di appears to be MEF or a derivative