Code Monkey home page Code Monkey logo

Comments (3)

Nab0y avatar Nab0y commented on May 25, 2024

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.

JerryNixon avatar JerryNixon commented on May 25, 2024

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.

JurajPupak avatar JurajPupak commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.