locked
Should each view have its own viewmodel, or should one big VM connect small views RRS feed

  • Question

  • User386791 posted

    I'm making settings page like this one on AliExpress app. If you click currency, it opens new modal page, where you select your preferred currency and when you click back, currency (EUR, USD, GBP,...) is updated and shown on the right side. Same for Language, Ship to and so on. So My question is, should every page have its own viewmodel or should I use one "big" viewmodel, let's say SettingsViewModel which I use for all settings?

    I'm working without any framework. What is the best way to do it?

    Thursday, September 5, 2019 1:05 PM

Answers

  • User369978 posted

    Another option : use Xamarin.Essentials: Preferences .

    Store value in currency page, get data every time (override OnAppearing method)you back to settings page.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 5, 2019 2:29 PM

All replies

  • User53115 posted

    I generally go with a one-to-one VM-to-Page ratio, but for something like this where it's just viewing vs editing the same data I think sharing the ViewModel is fine.

    Thursday, September 5, 2019 1:34 PM
  • User386791 posted

    @JoeManke How can I achieve this? Share one viewmodel with multiple views? Now I'm creating new instance of SettingsVM every time I open page

    Thursday, September 5, 2019 1:40 PM
  • User53115 posted

    I'm probably going to unleash the wrath of Clint for saying this, but I would pass the ViewModel from the main settings page to the edit page as a constructor argument.

    Thursday, September 5, 2019 1:49 PM
  • User76049 posted

    @JoeManke said: I'm probably going to unleash the wrath of Clint for saying this, but I would pass the ViewModel from the main settings page to the edit page as a constructor argument.

    Offfft, not only Clint's wrath :D

    Thursday, September 5, 2019 2:03 PM
  • User369978 posted

    Another option : use Xamarin.Essentials: Preferences .

    Store value in currency page, get data every time (override OnAppearing method)you back to settings page.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 5, 2019 2:29 PM