I'm new at Caliburn Micro and the MVVM pattern.
I have 3 views (with its corresponding viewmodels):
AppView
ClientsView
EditClientView
AppView
is my main page with a TabControl
inside. The child views (WPF UserControls
) are loaded in new tabs.
In ClientsView
I have a grid with filtered clients. I select one, and when the Modify
button is clicked I need to load a new tab with the editClientView
and load its data. Here is my code:
public class EventAggregationProvider
{
static EventAggregator _eventAggregator = null;
public static EventAggregator EventAggregator
{
get
{
if (_eventAggregator == null)
_eventAggregator = new EventAggregator();
return _eventAggregator;
}
}
}
Function on ClientsViewModel
:
public void ModificarCliente()
{
AppViewModel myParent = (AppViewModel)this.Parent;
myParent.OpenTab(typeof(modClienteViewModel));
EventAggregationProvider.EventAggregator.Publish(_selectedclient);
}
MainViewModel
OpenTab
function:
public void OpenTab(Type TipoVista)
{
bool bFound = false;
if (TipoVista != null)
{
Screen myScreen = (Screen)Activator.CreateInstance(TipoVista as Type);
myScreen.DisplayName = myScreen.ToString();
foreach (Screen miItem in Items)
{
if (miItem.ToString() == myScreen.ToString())
{
bFound = true;
ActivateItem(miItem);
}
}
if (!bFound) ActivateItem(myScreen);
}
}
EditClientViewModel
:
class modClienteViewModel : Screen, IHandle<vw_ClientesFull>
{
private OhmioService.OhmioServiceClient serviceClient =
new OhmioService.OhmioServiceClient();
public Clientes Cliente { get; set; }
public modClienteViewModel()
{
EventAggregationProvider.EventAggregator.Subscribe(this);
Cliente = new Clientes();
}
public void Handle(vw_ClientesFull myClient)
{
Cliente = serviceClient.Cliente_GetById(myClient.ID_Cliente);
NotifyOfPropertyChange("Cliente");
}
}
So what's your opinion on this? Especially about how I open the edit view (ClientsView
becomes aware of the existence of MainWindowViewmodel
and EditClientViewModel
) from clients' viewmodel, and how I use eventaggregator
to exchange the selected client between clientsViewModel
and EditClientViewModel
.