Click here to Skip to main content
15,895,656 members
Articles / Desktop Programming / WPF

Navigation in MVVM applications (Onyx)

Rate me:
Please Sign up or sign in to vote.
4.33/5 (7 votes)
13 Nov 2009CPOL4 min read 69.9K   2.2K   28  
Navigation in MVVM applications. Combined with navigation through DataTemplates, navigation through NavigationService provides a richer toolkit for WPF developers. I think it would be useful to utilize NavigationService, since the development of WPF applications then becomes similar to web developme
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Navigation;
using System.Windows;
using System.Windows.Input;

using Onyx;
using Onyx.ComponentModel;
using Onyx.Windows;

namespace NavigationDemo
{
    public class Page3ViewModel : ViewModel
    {
        public Page3ViewModel(View view)
            : base(view) {
                this.CommandBindings.Add(new CommandBinding(NavCommand, this.NavCommandExecute, new CanExecuteRoutedEventHandler(this.NavCommandCanExecute)));
        }

        #region NavCommand
        /// <summary>
        /// 
        /// </summary>
        private static readonly RoutedUICommand navCommand = new System.Windows.Input.RoutedUICommand("nav", "NavCommand", typeof(Page3ViewModel));
        /// <summary>
        /// 
        /// </summary>
        public static RoutedUICommand NavCommand
        {
            get { return navCommand; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NavCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void NavCommandExecute(object sender, ExecutedRoutedEventArgs args)
        {
            NavigationService service = NavigationService.GetNavigationService(this.View.ViewElement);

            try
            {
                // We use absolute Uri path, since it should be valid in both cases when run from local or remote assemblies
                Uri uri = new Uri("pack://application:,,,/NavigationDemo;component/" + args.Parameter.ToString(), UriKind.Absolute);
                service.Navigate(uri);
            }
            catch (Exception ex)
            {
            }
        }
        #endregion //navCommand
    }
}

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
Russian Federation Russian Federation
I have Master degree in Particle Physics. During my last several years I work as software developer.

Primary Interests
- c#, c++, php, java.
- scientific programming

Comments and Discussions