Question

My application is designed to load up an XML file and display an error(s) (if any).

The problem I have is how to display both (the XML and Errors) on screen without coupling (my application does currently work).

My application currently looks like (no laughing or comments about me going on a Photoshop\UI course please):

enter image description here

The brown colour is a different view called XmlView.
The red box is where I want errors to be displayed.

So, the user clicks File->Open, selects the file and the .XML content is displayed in my XmlView (brown) and my error messages are shown in red. This works but, I have a horrible feeling my design is poor as I have totally coupled my MainWindow and XmlView.

The way I have this working is, when the user selects a valid XML file (from File->Open), I create an instance of my XmlView and bind it to my Views property of my MainWindow class. My XmlView takes 1 parameter which is the MainWindow type.

So, within my XmlView, to update my ErrorList, I would write code similar to

_mainWindow.ErrorList.Add(//newError)

But this now means my XmlView knows about my MainWindow which I thought was undesired.

So, finally, my question! Is my design poor or is this OK?

Was it helpful?

Solution

You should consider using an MVVM framework if you are doing MVVM.

It would depend on whose responsibility it was to load the XML, but I would suggest the XmlViewModel, not the MainViewModel.

In that case the MainViewModel should just be a conductor of other view models. In your first case, it would instantiate the XmlViewModel, passing the file path and set it as its current view.

The XmlViewModel would be responsible for loading and validating the XML. It too could have a child view model which displays the validation errors. It should load the XML asynchronously, with some form of busy notification.

The MainViewModel is likely to want to conduct many view models, so if you were going to use a framework such as Caliburn.Micro, this would be a conductor type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top