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();
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 ;)