Implements the ICommand and wraps up all the verbose stuff so that you can just pass 2 delegates 1 for the CanExecute and one for the Execute
Inheritance: ICommand
        public RichTextViewModel()
        {
            Close = new SimpleCommand
            {
                //Publish a message with the new Text
                ExecuteDelegate = x => Mediator.NotifyColleagues(ViewModelMessages.HideRichText, RichTextBoxHelper.GetText((FlowDocument)x))
            };

            //Receive the Text from the previous form
            Mediator.Register(x => Text = x == null ? "" : x.ToString(), ViewModelMessages.ShowRichText);
        }
        public ProjectDetailsViewModel()
        {
            Project = new WPFDisciples.Backend.ProjectDetails();

            //initialize the ShowDevelopers Command
            ShowDevelopers = new SimpleCommand
            {
                //Notify listeners to show the developers View
                ExecuteDelegate = x => Mediator.NotifyColleagues(ViewModelMessages.ShowDevelopers, null)
            };

            ShowRichText = new SimpleCommand
            {
                //Notify listeners to show the rich text View
                ExecuteDelegate = x => Mediator.NotifyColleagues(ViewModelMessages.ShowRichText, x)
            };

            SaveProject = new SimpleCommand
            {
                ExecuteDelegate = Save,
                //Can execute only if there are no errors
                CanExecuteDelegate = x => String.IsNullOrEmpty(Project.Error)
            };

            Mediator.Register(
                x =>
                {
                    WPFDisciples.Backend.Developers dev = x as WPFDisciples.Backend.Developers;
                    if (dev != null)
                    {
                        Project.Developer = dev.Name;
                        Project.DeveloperId = dev.id;
                        Project.DeveloperPic = dev.Avatar;
                    }
                },
                ViewModelMessages.SelectDeveloper);

            //Receive a notification with the new Text that was entered in the RichText view for the Description
            Mediator.Register(x => Project.Description = x.ToString(), ViewModelMessages.HideRichText);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dispatcher">The dispatcher object of the view</param>
        public ProjectListViewModel(Dispatcher dispatcher)
        {
            this.dispatcher = dispatcher;

            //Register to the ShowListOfProjects message because this means that we added a new project and that we should add it to the list
            Mediator.Register(x =>
                //We could have got the data from the database, but the mediator will give us the new object and we can simple add it to the list and let ListBinding work it's magic :)
                    projects.Add((WPFDisciples.Backend.ProjectDetails)x),
                    ViewModelMessages.NewProjectAdded);


            OpenUrl = x => dispatcher.BeginInvoke(
                (Action)delegate
                {
                    try
                    {
                        System.Diagnostics.Process.Start((string)x);
                    }
                    catch
                    {
                        Mediator.NotifyColleagues(ViewModelMessages.ShowError, "Error opening URL");
                    }
                }, DispatcherPriority.Background, null);

            DeleteProject = new SimpleCommand
            {
                ExecuteDelegate = x =>
                {
                    var proj = (WPFDisciples.Backend.ProjectDetails)x;
                    if (ProjectsDataService.DeleteProject(proj))
                    {
                        Mediator.NotifyColleagues(ViewModelMessages.NotificationMessage, "Project deleted!");
                        Projects.Remove(proj);
                    }
                    else
                        Mediator.NotifyColleagues(ViewModelMessages.ShowError, "Cannot delete project!");

                }
            };

            //Switch the view name. This will be picked up by binding and the view will change accordingly
            SelectView = x => CurrentView = (string)x;
        }
Exemple #4
0
        /// <summary>
        /// Guess what.... this is the Constructor... LOL sorry about this comment, I guess I have nothing better to do :D
        /// </summary>
        public MainViewModel()
        {
            //Register for notification to show or Hide the Developers View
            Mediator.Register(x => ShowDevelopers = true, ViewModelMessages.ShowDevelopers);
            Mediator.Register(x => ShowDevelopers = false, ViewModelMessages.SelectDeveloper);

            //Register for notification to show or Hide the RichText View
            Mediator.Register(x => ShowRichText = true, ViewModelMessages.ShowRichText);
            Mediator.Register(x => ShowRichText = false, ViewModelMessages.HideRichText);

            //Register for notification to show the Errors View
            Mediator.Register(x => Error = x.ToString(), ViewModelMessages.ShowError);

            //Initialize the action to Hide the errors View
            HideError = x => Error = null;

            Mediator.Register(x => NotificationMessage = x.ToString(), ViewModelMessages.NotificationMessage);

            //Initialize the action to Hide the notification View
            HideNotification = x => NotificationMessage = null;

            VisitTheDisciples = x => Dispatcher.CurrentDispatcher.BeginInvoke(
                (Action)delegate { System.Diagnostics.Process.Start("http://groups.google.com/group/wpf-disciples"); }, DispatcherPriority.Background, null);

            //Switch the view name. This will be picked up by binding and the view will change accordingly
            SelectView = new SimpleCommand
            {
                ExecuteDelegate = x => CurrentView = (string)x
            };
        }