Skip to content

Instantly share code, notes, and snippets.

@tarasn
Created August 18, 2015 11:15
Show Gist options
  • Save tarasn/55079e824b64e4e0e072 to your computer and use it in GitHub Desktop.
Save tarasn/55079e824b64e4e0e072 to your computer and use it in GitHub Desktop.
Caliburn.Micro+Castle.Windsor navigation example(boilerplate)
<Application x:Class="CaliburnWpfAppCaliburn.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CaliburnWpfAppCaliburn">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:AppBootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
using System;
using System.Collections.Generic;
using System.Windows;
using Caliburn.Micro;
using Castle.Windsor;
using System.Linq;
using Castle.MicroKernel.Registration;
namespace CaliburnWpfAppCaliburn
{
public class AppBootstrapper : BootstrapperBase
{
private IWindsorContainer _container;
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
_container = new WindsorContainer();
_container.Register(Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifestyleSingleton());
_container.Register(Component.For<ShellViewModel>().ImplementedBy<ShellViewModel>().LifestyleSingleton());
_container.Register(Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifestyleSingleton());
//_container.Singleton<IWindowManager, WindowManager>();
//container.Singleton<IEventAggregator, EventAggregator>();
//container.PerRequest<IScreen, ShellViewModel>();
}
protected override object GetInstance(Type service, string key)
{
var instance = _container.Resolve(service);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
var array = _container.ResolveAll(service);
object[] trgArray = new object[array.Length];
Array.Copy(array, trgArray, array.Length);
return trgArray;
}
//protected override void BuildUp(object instance)
//{
// _container.BuildUp(instance);
//}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
}
}
<UserControl x:Class="CaliburnWpfAppCaliburn.MyBestView"
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"
d:DesignHeight="300" d:DesignWidth="300" Background="Chocolate">
<Grid>
</Grid>
</UserControl>
<Window x:Class="CaliburnWpfAppCaliburn.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" x:Name="CheckWeb" Content="Check Web" />
<Button Grid.Row="1" x:Name="CheckService" Content="Check Service" />
<ContentControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" x:Name="ActiveItem" Background="Yellow"/>
</Grid>
</Window>
using Caliburn.Micro;
namespace CaliburnWpfAppCaliburn {
public class SomeViewModel : Screen
{
}
public class MyBestViewModel : Screen
{
protected override void OnActivate()
{
}
protected override void OnDeactivate(bool close)
{
}
}
public class ShellViewModel : Conductor<object>.Collection.OneActive
{
private readonly IWindowManager _windowManager;
private readonly IEventAggregator _eventAggregator;
public ShellViewModel(IWindowManager windowManager,IEventAggregator eventAggregator)
{
_windowManager = windowManager;
_eventAggregator = eventAggregator;
}
public bool CanCheckWeb()
{
return true;
}
public void CheckWeb()
{
this.ActivateItem(new SomeViewModel());
}
public bool CanCheckService()
{
return true;
}
public void CheckService()
{
this.ActivateItem(new MyBestViewModel());
}
}
}
<UserControl x:Class="CaliburnWpfAppCaliburn.SomeView"
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"
d:DesignHeight="300" d:DesignWidth="300" Background="Blue">
<Grid>
</Grid>
</UserControl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment