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

MVVM for Multi Platforms

, 22 Mar 2010
How to implement MVVM when developing a view model whose view implementation language is not certain
GeographicRepresentation.zip
GeographicRepresentation
bin
Release
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Interfaces.pdb
GeographicRepresentation.Lib.dll
GeographicRepresentation.Lib.pdb
GeographicRepresentation.TestLib.dll
GeographicRepresentation.TestLib.pdb
GeographicRepresentation.UILib.exe
GeographicRepresentation.UILib.pdb
GeographicRepresentation.UILib.vshost.exe
GeographicRepresentation.UILib.vshost.exe.manifest
Resources
GeographicRepresentation.suo
GeographicRepresentation.TestLib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
GeographicRepresentation.TestLib.dll
Release
Properties
GeographicRepresentation.vsmdi
Interfaces
bin
Debug
GeographicRepresentation.Interfaces.dll
Release
Properties
Lib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
Release
Model
Properties
Resources
ViewModel
LocalTestRun.testrunconfig
TestResults
UILib
bin
Debug
GeographicRepresentation.UILib.exe
WpfApplication1.vshost.exe.manifest
Release
GeographicRepresentation.UILib.vshost.exe.manifest
Properties
Settings.settings
UILib.csproj.user
View
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

using GeographicRepresentation.Interfaces;
using GeographicRepresentation.Lib;

namespace GeographicRepresentation.UILib.View
{
    /// <summary>
    /// Interaction logic for ContinentsViewGrid.xaml
    /// </summary>
    public partial class ContinentsViewGrid : Grid, INotifyPropertyChanged
    {
        #region Members

        IReadOnlyViewModel m_SelectedObject;        

        #endregion

        #region Properties

        public IReadOnlyViewModel SelectedObject
        {
            get { return m_SelectedObject; }
            set
            {
                m_SelectedObject = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedObject"));
                }
            }
        }        

        #endregion

        #region Methods

        #region Ctor

        public ContinentsViewGrid()
        {
            InitializeComponent();
            m_SelectedObject = null;
        }

        #endregion

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            IContinentsListViewModel vm = InterfacesFactory.Instance.GetContinentsListViewModel();
            this.DataContext = vm;
            SelectedObject = vm.Continents[0];
            
        }

        private void continentsView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {            
            // When the selected item changes, unregister the changed event
            UnregisterChangedDataEvent();
            SelectedObject = e.NewValue as IReadOnlyViewModel;
            // Register the new selected object to a changed data event
            RegisterChangedDataEvent();            
        }

        private void UnregisterChangedDataEvent()
        {
            if (m_SelectedObject != null)
            {
                Type[] types = m_SelectedObject.GetType().GetInterfaces();                
                
                if (types.Contains(typeof(IReadOnlyContinentViewModel)))
                {
                    IReadOnlyContinentViewModel vm = m_SelectedObject as IReadOnlyContinentViewModel;
                    vm.OnBestCityToBeInChanged -= this.DataChanged;
                }
                else if (types.Contains(typeof(IReadonlyCountryViewModel)))
                {
                    IReadonlyCountryViewModel vm = m_SelectedObject as IReadonlyCountryViewModel;
                    vm.OnBirthRateChanged -= this.DataChanged;
                }
            }
        }

        private void RegisterChangedDataEvent()
        {
            if (m_SelectedObject != null)
            {
                Type[] types = m_SelectedObject.GetType().GetInterfaces();                

                if (types.Contains(typeof(IReadOnlyContinentViewModel)))
                {
                    IReadOnlyContinentViewModel vm = m_SelectedObject as IReadOnlyContinentViewModel;
                    vm.OnBestCityToBeInChanged += this.DataChanged;
                }
                else if (types.Contains(typeof(IReadonlyCountryViewModel)))
                {
                    IReadonlyCountryViewModel vm = m_SelectedObject as IReadonlyCountryViewModel;
                    vm.OnBirthRateChanged += this.DataChanged;
                }                
            }
        }

        /// <summary>
        /// A Change in the data would carry data update
        /// </summary>
        private void DataChanged()
        {
            SelectedObject = m_SelectedObject;            
        }                

        /// <summary>
        /// Call for updating the data. This set an asynchronos call for the view model
        /// </summary>
        public void UpdateData()
        {
            IContinentsListViewModel vm = this.DataContext as IContinentsListViewModel;
            SimpleOperationDelegate delg = new SimpleOperationDelegate(vm.LoadContinentsFromRepository);
            delg.BeginInvoke(this.UpdateDataCompleted, delg);
        }

        /// <summary>
        /// Call back for the data reloading. The update should take place in the main thread
        /// </summary>
        /// <param name="result"></param>
        private void UpdateDataCompleted(IAsyncResult result)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new AsyncCallback(this.UpdateDataCompletedInMainThread), result);
        }

        /// <summary>
        /// Update the view
        /// </summary>
        /// <param name="result"></param>
        private void UpdateDataCompletedInMainThread(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                continentsView.Items.Refresh();
                SimpleOperationDelegate delg = result.AsyncState as SimpleOperationDelegate;
                delg.EndInvoke(result);
            }
        }

       

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion  

        #endregion
        
    }    
}

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)

About the Author

Izhar Lotem
Software Developer
Israel Israel
Software Developer in a promising Clean-Tech company

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Mar 2010
Article Copyright 2010 by Izhar Lotem
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid