Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

i'm writing an application using PRISM. I have a few long running processes where i need to give feedback to the ui. For the purpose of seperation i want to seperate the processcode from my viewmodels. at the moment, i am using classes that implement the ICommand interface and the NotificationObject and run them asynchronusly. For example:

C#
class ImportMatFileCommand : ViewModelBase, ICommand
    {
        #region Members

        private ImportFileViewModel materialFile;
        private ObservableCollection<ImportErrorViewModel> errors = new ObservableCollection<ImportErrorViewModel>();

        private Int32 numberOfLines = 1;
        private Int32 processedLines = 0;

        #endregion

        #region ICommand Members

        /// <summary>
        /// Whether the ShowModuleAViewCommand is enabled.
        ///</summary>
        public bool CanExecute(object param)
        {
           //canexecute
        }

        /// <summary>
        /// Actions to take when CanExecute() changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Executes the ShowModuleAViewCommand
        /// </summary>
        public async void Execute(object param)
        {
            await ImportAsync();
        }

        #endregion

        #region Properties

        public ImportFileViewModel MaterialFile
        {
            get
            {
                return materialFile;
            }
            set
            {
                materialFile = value;

                base.RaisePropertyChangedEvent("MaterialFile");
            }
        }

        public ObservableCollection<ImportErrorViewModel> Errors
        {
            get
            {
                return errors;
            }
            set
            {
                errors = value;
                base.RaisePropertyChangedEvent("Errors");
            }
        }

        public Int32 NumberOfLines
        {
            get
            {
                return numberOfLines;
            }
            set
            {
                numberOfLines = value;
                base.RaisePropertyChangedEvent("NumberOfLines");
            }
        }

        public Int32 ProcessedLines
        {
            get
            {
                return processedLines;
            }
            set
            {
                processedLines = value;
                base.RaisePropertyChangedEvent("ProcessedLines");
            }
        }

        #endregion

        #region Private Methods

        private Task ImportAsync()
        {
            return Task.Run(() => Import());
        }

        private void Import()
        {
           //long running process....
        }
    }


This works but i don't know if this is a good approach. Actually I want to use the delegatecommand. I know how to use the delegate command but i dont know the best approach to seperate the processcode from my viewmodel, so that i can have a projectstructure like this:

-MyProject
--Commands
--ViewModels
--Views
--...
Posted
Updated 17-Jun-13 3:26am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900