Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF PRISM
Hi,
 
in my masterthesis i decided to use some .net techniques. The appication is written in c#/wpf with the prism library regarding to architekture. Now i got a problem on the feedback of single commands, seperated executed in a composite command.
 
I want to import some huge files for my model so i need some kind of feedback. I have written an ICommand for each file and made it asynchronously running with the async/await operators. An example of these files look like this:
 

class ImportMaterialFile : ICommand
{
    #region Members
 
    ImportFileViewModel materialFile;
    public ObservableCollection<ImportErrorViewModel> errors = new ObservableCollection<ImportErrorViewModel>();
 
    Int16 numberOfLines = 0;
    Int16 processedLines = 0;
 
    #endregion
 
    #region ICommand Members
 
    public bool CanExecute(object param)
    {
        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        InterfaceViewModel interfaceViewModel = (InterfaceViewModel)container.Resolve<IInterfaceViewModel>();
 
        bool CanExecute = false;
 
        foreach (ImportFileViewModel importFile in interfaceViewModel.SapFeliosInterfaceFiles)
        {
            if (importFile.FileName.Split('.').First().Contains("MAT"))
            {
                materialFile = importFile;
                try
                {
                    StreamReader reader = new StreamReader(materialFile.FilePath);
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        numberOfLines++;
                    }
                    CanExecute = true;
                }
                catch (Exception ex)
                {
                    errors.Add(new ImportErrorViewModel(ex.ToString(), materialFile.FileName, 0));
                }
            }
        }
 
        return CanExecute;
    }
 
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
 
    public async void Execute(object param)
    {
        await ImportAsync();
    }
 
    #endregion
 
    #region Private Methods
 
    private Task ImportAsync()
    {
        return Task.Run(() => Import());
    }
 
    private void Import()
    {
        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        InterfaceViewModel interfaceViewModel = (InterfaceViewModel)container.Resolve<IInterfaceViewModel>();
 
        try
        {
            StreamReader streamReader = new StreamReader(materialFile.FilePath);
            String line;
            Int16 counter = 1;
 
            Material material;
 
            while ((line = streamReader.ReadLine()) != null)
            {
                try
                {
                    material = new Material();
 
                    //set properties from line

                    interfaceViewModel.ModelSapFeliosContainer.MaterialSet.Add(material);
 
                    counter++;
                    processedLines++;
                }
                catch (Exception ex)
                {
                    errors.Add(new ImportErrorViewModel(ex.ToString(), materialFile.FileName, counter));
                }
            }
 
            interfaceViewModel.ModelSapFeliosContainer.SaveChanges();
        }
        catch (Exception ex)
        {
            MessageBox.Show("error while reading MAT-File\n\n" + ex.ToString());
        }
    }
 
    #endregion
}
 
I have to execute three of these files in a compositecommand which looks like this.
 
class ImportSapFeliosInterfaceFiles : CompositeCommand
    {
        #region Members
 
        ImportProcessingPointFile importProcessingPointFile = new ImportProcessingPointFile();
        ImportMaterialFile importMaterialFile = new ImportMaterialFile();
        ImportProductionOrderFile importProductionOrderFile = new ImportProductionOrderFile();
        ImportProductionOrderStepFile importProductionOrderStepFile = new ImportProductionOrderStepFile();
 
        ObservableCollection<ImportErrorViewModel> errors = new ObservableCollection<ImportErrorViewModel>();
        
        #endregion
 
        #region Construction
 
        public ImportSapFeliosInterfaceFiles()
        {
            this.RegisterCommand(importMaterialFile);
            this.RegisterCommand(importProductionOrderFile);
            this.RegisterCommand(importProductionOrderStepFile);
        }
 
        #endregion
 
        #region Properties
 
        public ObservableCollection<ImportErrorViewModel> GetErrors()
        {
            foreach (ImportErrorViewModel ie in importMaterialFile.errors)
            {
                errors.Add(ie);
            }
 
            foreach (ImportErrorViewModel ie in importProductionOrderFile.errors)
            {
                errors.Add(ie);
            }
 
            foreach (ImportErrorViewModel ie in importProductionOrderStepFile.errors)
            {
                errors.Add(ie);
            }
 
            return this.errors;
        }
 
        #endregion
    }
 
Now, my actual Problem is, that i want to have one View, giving me a feedback on whats going on. I cant figure out, how to to this. Is this the point where i have to use prisms eventaggregator? Or can i just signature my ICommands as an observableobject?
 
Some tips, ideas etc would be nice Wink | ;)
Posted 10-Jun-13 2:49am

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 265
1 Sergey Alexandrovich Kryukov 202
2 George Jonsson 185
3 King Fisher 185
4 CPallini 155
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 10 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100