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

MVVM for Multi Platforms

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
22 Mar 2010CPOL2 min read 26.3K   354   22  
How to implement MVVM when developing a view model whose view implementation language is not certain
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)


Written By
Software Developer
Israel Israel
Software Developer in a promising Clean-Tech company

Comments and Discussions