Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming / WPF

Model-View-ViewModel (MVVM) Explained

Rate me:
Please Sign up or sign in to vote.
4.94/5 (259 votes)
8 Aug 2010CPOL23 min read 2.3M   26K   710  
An introduction to the Model-View-ViewModel (MVVM) pattern.
using System;
using System.Linq;
using System.Windows.Threading;

namespace MVVMExample
{
    public class Service : IService 
    {
        public void GetContacts(Action<IQueryable<ContactModel>> callback)
        {
            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += (o, e) =>
                              {
                                  ((DispatcherTimer) o).Stop();
                                  callback(ContactDb.GetContacts());
                              };
            timer.Start();
        }

        public void DeleteContact(ContactModel contact)
        {
            ContactDb.Delete(contact);            
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager Microsoft
United States United States
Note: articles posted here are independently written and do not represent endorsements nor reflect the views of my employer.

I am a Program Manager for .NET Data at Microsoft. I have been building enterprise software with a focus on line of business web applications for more than two decades. I'm the author of several (now historical) technical books including Designing Silverlight Business Applications and Programming the Windows Runtime by Example. I use the Silverlight book everyday! It props up my monitor to the correct ergonomic height. I have delivered hundreds of technical presentations in dozens of countries around the world and love mentoring other developers. I am co-host of the Microsoft Channel 9 "On .NET" show. In my free time, I maintain a 95% plant-based diet, exercise regularly, hike in the Cascades and thrash Beat Saber levels.

I was diagnosed with young onset Parkinson's Disease in February of 2020. I maintain a blog about my personal journey with the disease at https://strengthwithparkinsons.com/.


Comments and Discussions