I'm working on a WPF application and I want to integrate Caliburn Micro. In the main page I have a content presenter which is loaded with a UserControl (let`s say is named MenuView as you can see below) when application starts. But when I try to handle a MouseDown event using interaction in MenuView, the method is never called in MenuViewModel, because probably the viewmodel is not registed in Caliburn.
Here is the application structure:
MainViewModel:
public class MainViewModel : PropertyChangedBase, IShell
{
public MainViewModel()
{
SelectedView = new MenuView();
}
private UserControl _selectedView;
public UserControl SelectedView
{
get { return _selectedView; }
set
{
_selectedView = value;
NotifyOfPropertyChange();
}
}
}
MainView:
<Window x:Class="UITablet.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowState="Maximized" WindowStyle="ThreeDBorderWindow">
<StackPanel VerticalAlignment="Bottom">
<ContentPresenter Content="{Binding SelectedView, Mode=TwoWay}" />
</StackPanel>
</Window>
MenuViewModel:
public class MenuViewModel : IShell
{
public void RedirectToSapAction()
{
//...
}
}
MenuView:
<UserControl x:Class="UI.Tablet.Views.MenuView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:cal="http://www.caliburnproject.org"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button x:Name="TestButton" Width="100" Height="20" Content="Click me!">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cal:ActionMessage MethodName="RedirectToSapAction" >
</cal:ActionMessage >
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</UserControl>
And finally the AppBootstrapper
using System.IO;
using System.Linq;
using System.Reflection;
using WpfApplication1.ViewModels;
using System;
using System.Collections.Generic;
using Caliburn.Micro;
namespace WpfApplication1
{
public class AppBootstrapper : BootstrapperBase
{
SimpleContainer container;
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
ViewLocator.AddSubNamespaceMapping("WpfApplication1.ViewModels", "UITablet.Views");
container = new SimpleContainer();
container.Singleton<IWindowManager, WindowManager>();
container.Singleton<IEventAggregator, EventAggregator>();
container.PerRequest<IShell, MainViewModel>();
}
protected override object GetInstance(Type service, string key) {
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service) {
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance) {
container.BuildUp(instance);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {
DisplayRootViewFor<IShell>();
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
var assemblies = new List<Assembly>();
assemblies.AddRange(base.SelectAssemblies());
//Load new ViewModels here
string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory());
assemblies.AddRange(from fileName in fileEntries where fileName.Contains("UITablet.dll") select Assembly.LoadFile(fileName));
return assemblies;
}
}
}
As you can see, the views are placed in a separate project and I`ve done some tricks in order to make Caliburn functional. The question is, this separation is the problem? or am I missing something?
In your main view model, define a property of type MenuViewModel
private MenuViewModel_selectedView;
public MenuViewModel SelectedView
{
get { return _selectedView; }
set
{
_selectedView = value;
NotifyOfPropertyChange();
}
}
You can instantiate the SelectedView
property in the constructor or whenever it's necessary.
In your view:
<ContentPresenter cal:View.Model="{Binding SelectedView, Mode=TwoWay}">
</ContentPresenter>