Comments (3)
I use a DI container in my case Unity and Locator pattern of MVVM Light.
Register the ViewModel in locator constructor
unityContainer.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager());
new ContainerControlledLifetimeManager()
- important part provides one instance
Getting the ViewModel from Locator.
public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>();
Locator as a resource app.xaml (actually a BootStrapper from T10)
<common:BootStrapper
x:Class="Dab.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Dab"
xmlns:common="using:Template10.Common"
RequestedTheme="Dark">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Custom.xaml"/>
<ResourceDictionary Source="Styles/DataTemplates.xaml"/>
<ResourceDictionary Source="Styles/Mdl2.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:Dab.ViewModel" />
</ResourceDictionary>
</Application.Resources>
</common:BootStrapper>
The ViewModel as the DataContext for the Model
<view:MainPageBase
x:Class="Dab.View.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:view="using:Dab.View"
mc:Ignorable="d"
DataContext="{Binding Path=Main, Source={StaticResource Locator}}">
...
from template10.
Let's start with this: typically, in MVVM, there is a one to one for view and viewmodel. This means you would never use the MainPageViewModel, for example, for the DetailsPage even if the data were identical. This is true for several reasons, the least of which is convention.
If what you mean, however, is how to reuse the viewmodel in multiple instances of the same view, then there are several techniques for this. Using inversion of control, a dependency injection container can be instructed to treat a viewmodel as a singleton or to create a new one every time.
In addition, it's worth pointing out that ViewModelBase now has a property in Template10 called SessionState that allows you to persist instances of a viewmodel in memory while you either 1) pass a pointer to it using parameters or 2) use a second instance for a point in time, then return to it.
So, how do you reuse the same viewmodel for multiple types of views? I would recommend that you do not. If you feel there is too much code duplication then use subclassing so you can maintain the overall good practice of one to one viewmodels without much duplication. If you mean reusing a view for a second instance of a view you can instruct your container to handle that and store a reference to the previous viewmodel in SessionState if you want to keep it.
from template10.
I found this question, because I have same problem, same dilemma. What If I have 2 Views (let's say LoginView_Part1, LoginView_Part2). Now I have 2 VM's, one for each View. But I'm thinking to combine the VM's into one VM, because the Login view is only splitted into 2 views. And some data from first View I need in second View. Should I send them as parameter in Navigate method (so I would have 2 VM), or should I create one VM shared for the Views?
from template10.
Related Issues (20)
- Discussion: How to handle Background Tasks. HOT 1
- Unable load T10 project form GitHub HOT 13
- Add ToUri() to the PathBuilder HOT 4
- Navigation Services Not working HOT 1
- Reconsider if ViewModelBase should implement NavigationService HOT 1
- Why haven't we implemented auto-discover for Nav/Pages? HOT 1
- Confirm revert projects back to UWP Class Library
- Misleading information about Prism HOT 1
- Misleading exception while failing to deserialize an argument is passed in Navigation Service HOT 1
- Xaml exception (suddenly, without history) HOT 2
- AttachGestures should have an override without Window
- Add Nav extensions off Page types HOT 1
- GestureService shouldn't handle the BackRequested from SystemNavigationManager
- Exception handling in SettingsService HOT 3
- How can I use Navigation Service without Template 10 Bootstrapper? HOT 2
- Any news about future? HOT 1
- I need access to old Nuget packages HOT 1
- Update to reference latest version of Newtonsoft.Json library HOT 2
- Windows 10
- Template 10 ellipsis collapsed error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from template10.