Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

An Address Book Application Made in MVVM For Metro Style App in Windows 8 Part 2

, 29 May 2012 CPOL
This is the Second Part XML file as data source in Windows8
Mvvm3_Full.zip
Mvvm3_Basic
Mvvm3_Basic
Assets
Logo.png
SmallLogo.png
SplashScreen.png
StoreLogo.png
bin
ARM
Debug
Release
Debug
AppX
Assets
Logo.png
SmallLogo.png
SplashScreen.png
StoreLogo.png
Common
microsoft.system.package.metadata
S-1-5-21-3520127098-591485480-2378297468-1001.pckgdep
Mvvm3_Basic.exe
Mvvm3_Basic.pdb
resources.pri
vs.appxrecipe
Assets
Logo.png
SmallLogo.png
SplashScreen.png
StoreLogo.png
Common
Mvvm3_Basic.build.appxrecipe
Mvvm3_Basic.exe
Mvvm3_Basic.pdb
resources.pri
Release
x64
Debug
Release
x86
Debug
Release
Common
Model
Mvvm3_Basic_TemporaryKey.pfx
obj
Debug
Common
DesignTimeResolveAssemblyReferencesInput.cache
intermediatexaml
Mvvm3_Basic.exe
Mvvm3_Basic.pdb
LanguageQualifiers.txt.intermediate
layout.resfiles
layout.resfiles.intermediate
Mvvm3_Basic.exe
Mvvm3_Basic.pdb
pri.resfiles
pri.resfiles.intermediate
priconfig.xml.intermediate
resources.resfiles
resources.resfiles.intermediate
TempPE
Package.appxmanifest
Properties
ViewModel
Mvvm3_Basic.v11.suo
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Mvvm3_Basic.Model;

namespace Mvvm3_Basic.ViewModel
{
    public class ContactViewModel : INotifyPropertyChanged
    {
        public ContactViewModel()
        {
            AddCommand = new MyCommand<object>(OnAdd);
            SaveCommand = new MyCommand<object>(OnSave);
            UpdateCommand = new MyCommand<object>(OnUpdate);
            DeleteCommand = new MyCommand<object>(OnDelete);
            GetCommand = new MyCommand<object>(GetContact);
        }

        public ObservableCollection<Contact> Contacts { get; set; }
        
        public MyCommand<object> AddCommand { get; set; }
        public MyCommand<object> SaveCommand { get; set; }
        public MyCommand<object> UpdateCommand { get; set; }
        public MyCommand<object> DeleteCommand { get; set; }
        public MyCommand<object> GetCommand { get; set; }

        void OnAdd(object obj)
        {
            Contact c = new Contact { Name = string.Empty, Email = string.Empty };
            SelectedContact = c;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedContact"));

            Debug.WriteLine("Add Called!");
        }

        void OnSave(object obj)
        {
            db.Save(SelectedContact);
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedContact"));

            Debug.WriteLine("Save Called!");
        }

        void OnUpdate(object obj)
        {
            db.Update(SelectedContact);
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Contacts"));

            Debug.WriteLine("Update Called!");
        }
        
        void OnDelete(object obj)
        {
            db.Delete(SelectedContact);
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedContact"));

            Debug.WriteLine("Delete Called!");
        }
        
        void GetContact(object obj)
        {
            if (Contacts == null) Contacts = new ObservableCollection<Contact>();
            Contacts.Clear();
            db.GetContactList();
            Contacts = db.contactlist;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Contacts"));

            Debug.WriteLine("Refresh called!");
        }

        DB db = new DB();    //at class level

        private Contact _ctc = new Contact();
        public Contact SelectedContact
        {
            get { return _ctc; }
            set
            {
                _ctc = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedContact"));

            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class MyCommand<T> : ICommand
    {
        readonly Action<T> callback;

        public MyCommand(Action<T> callback)
        {
            this.callback = callback;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (callback != null) { callback((T)parameter); }
        }
    }
}

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)

Share

About the Author

santosh_pathak
CEO Addya Technologies
India India
"I am the CEO"
An Entrepreneur, Driving an Business Transformation Unit.
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 29 May 2012
Article Copyright 2012 by santosh_pathak
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid