What's the best way to expose a Model object in a ViewModel?

michalgizamichalgiza Member ✭✭
edited December 2018 in Xamarin.Forms

Hi developers ! :smile:

I'm begginer into xamarin development and also consider myself as beginner programmist. For some time i'm trying to refactor my app which i wrote first without any design approach or deeper thinking about adding more features to it. Now i face a problem with applying mvvm design pattern which i want to learn before i will start to looking for some internship. I read many tutorials (10 pages next in google ) seen about 50 tutorials on youtube :cold_sweat: and all of them are missing one important thing for me or i just not understoodem well.I tired to expose my model in view model but i don't figure it out how to do it properly becouse i still seeing on output console that my command hasn't found a property bound to it.

How to expose model properties in view model to be able to menage them in view model. ? I want to be able at least increasing one property value of my object by command in view model ? How to menage with this problem ? If you have some links or any your explanation i would be very greatefull for that or just sample example. Any help very appreciate. :smile:
I tired to expose my model in view model but i don't figure it out how to do it properly becouse i still seeing on output console that my command hasn't found a property bound to it.

My final tried before asking here :

        public Waste _wasteModel {
            get
            {
                return _waste;
            }
            set
            {
                _waste = value;
                OnPropertyChanged();
            }
        }

public double Counter
        {
            get
            {
                return _waste.Counter;
            }
            set
            {
                _waste.Counter = value;
                OnPropertyChanged();
            }
        }

Best Answer

Answers

  • NMackayNMackay GBInsider, University mod
    edited December 2018

    Reformat your message and enclose the the code with three ` on each side of the code block, otherwise it's unreadable.

     public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                MainPage = new NavigationPage(new DroidPageTitleBug.MainPage());
            }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    No idea what you mean by "expose" the properties of the model.
    Why are you making properties on the ViewModel that are copies of the properties on the Model such as:
    Waste.Counter? We don't do that.

    Please try building the sample app from here:
    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

    Don't just read it quickly looking for an answer. Stop. Work the series from start to end - it won't take even 2 hours. That should help you get a better understanding.

  • michalgizamichalgiza Member ✭✭

    i edited it to be more readable :smile: sorry

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @michalgiza said:
    i edited it to be more readable :smile: sorry

    Did you look at it after? Does it look better?

    Please read this article
    https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    Then please fix your post correctly so the code looks like code.

  • michalgizamichalgiza Member ✭✭

    Done :smile: By exposing properties maybe i cut my thought to short. I meant that i'm not sure what is the common practise or approved way to working on properities of my model in view model. How to represent them ? Is that so simple that i just need to create a same var in view model and start to play with ? :confused: :mask:

Sign In or Register to comment.