Click here to Skip to main content
11,488,877 members (78,498 online)
Click here to Skip to main content
Add your own
alternative version

MVVM for Multi Platforms

, 22 Mar 2010 CPOL 14.4K 288 22
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 GeographicRepresentation.Interfaces;
using GeographicRepresentation.Lib;

namespace GeographicRepresentation.UILib.View
{
    /// <summary>
    /// Interaction logic for EditContinentWindow.xaml
    /// </summary>
    public partial class EditContinentWindow : Window
    {
        #region Members

        Guid m_ContinentId;
        IEditableContinentViewModel m_DataContext;

        public static RoutedUICommand cmd_SaveContinent = new RoutedUICommand("saveContinent", "saveContinent", typeof(EditContinentWindow));
        public static RoutedUICommand cmd_Cancel = new RoutedUICommand("cancelContinent", "cancelContinent", typeof(EditContinentWindow));

        #endregion

        #region Methods

        #region Ctor        
        
        public EditContinentWindow()
        {
            m_ContinentId = Guid.Empty;
            InitializeComponent();
        }

        /// <summary>
        /// Ctor for editing the continent
        /// </summary>
        /// <param name="continentId"></param>
        public EditContinentWindow(Guid continentId) : this()
        {
            m_ContinentId = continentId;
        }

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SimpleOperationDelegate delg = new SimpleOperationDelegate(this.GetDataContext);
            delg.BeginInvoke(new AsyncCallback(this.DataContextLoaded), delg);

            if (m_ContinentId == Guid.Empty)
            {
                this.Title = "Add continent";
            }

            CommandBinding cb = new CommandBinding(cmd_SaveContinent, onSave, canSave);
            CommandBindings.Add(cb);
            cb = new CommandBinding(cmd_Cancel, onCancel, canCancel);
            CommandBindings.Add(cb);
        }

        /// <summary>
        /// A request for the data context
        /// </summary>
        private void GetDataContext()
        {
            if (m_ContinentId == Guid.Empty)
            {
                m_DataContext = InterfacesFactory.Instance.GetEditableContinentViewModelForAdd();
            }
            else
            {
                m_DataContext = InterfacesFactory.Instance.GetEditableContinentViewModelForEdit(m_ContinentId);
            }
        }

        /// <summary>
        /// The data context call back. Make all the adjustments in the main thread, using the dispatcher
        /// </summary>
        /// <param name="result"></param>
        private void DataContextLoaded(IAsyncResult result)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new AsyncCallback(this.DataContextLoadedInMainThread), result);
        }

        /// <summary>
        /// Update the view
        /// </summary>
        /// <param name="result"></param>
        private void DataContextLoadedInMainThread(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                this.DataContext = m_DataContext;
                SimpleOperationDelegate delg = result.AsyncState as SimpleOperationDelegate;
                delg.EndInvoke(result);
                Button saveButton = (Button)this.Template.FindName("saveButton", this);
                if (saveButton != null)
                {
                    saveButton.Command = EditContinentWindow.cmd_SaveContinent;
                }
                Button cancelButton = (Button)this.Template.FindName("cancelButton", this);
                if (cancelButton != null)
                {
                    cancelButton.Command = EditContinentWindow.cmd_Cancel;
                }
                if (m_ContinentId != Guid.Empty)
                {
                    this.Title = string.Format("Edit {0}", m_DataContext.ContinentName);
                }
            }
        }

        private void onSave(object sender, ExecutedRoutedEventArgs args)
        {
            this.Cursor = Cursors.Wait;
            SimpleOperationDelegate delg = new SimpleOperationDelegate(this.SaveOperation);
            delg.BeginInvoke(this.OperationCompleted, delg);
        }

        /// <summary>
        /// Textual data validation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void canSave(object sender, CanExecuteRoutedEventArgs args)
        {
            int nNum;
            args.CanExecute = txtName.Text.Length > 0 &&
                              txtNumOfCountries.Text.Length > 0 &&
                              int.TryParse(txtNumOfCountries.Text, out nNum) &&
                              nNum > 0;
        }

        private void SaveOperation()
        {
            m_DataContext.Save();
        }

        /// <summary>
        /// The operation call back. Call the dispatcher to update the view from the main thread
        /// </summary>
        /// <param name="result"></param>
        private void OperationCompleted(IAsyncResult result)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new AsyncCallback(this.EndOperation), result);
        }

        /// <summary>
        /// End the asynchronos call
        /// </summary>
        /// <param name="result"></param>
        private void EndOperation(IAsyncResult result)
        {
            this.Cursor = Cursors.Arrow;

            SimpleOperationDelegate delg = result.AsyncState as SimpleOperationDelegate;
            delg.EndInvoke(result);
            this.DialogResult = true;
            this.Close();            
        }

        /// <summary>
        /// The cancelation button should run if this is the edit mode, othewise, simply close the window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void onCancel(object sender, ExecutedRoutedEventArgs args)
        {
            if (m_ContinentId != Guid.Empty)
            {
                this.Cursor = Cursors.Wait;
                SimpleOperationDelegate delg = new SimpleOperationDelegate(this.CancelOperation);
                delg.BeginInvoke(this.OperationCompleted, delg);
            }
            else
            {
                this.Close();
            }
        }


        private void canCancel(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = true;
        }

        private void CancelOperation()
        {
            m_DataContext.Cancel();
        }

        #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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 22 Mar 2010
Article Copyright 2010 by Izhar Lotem
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid