I try to use Caliburn Micro in WPF application in combination with modern UI. I find some solutions which uses caliburn content loader however it doesn't work at all, here is the code:
public class CaliburnContentLoader : DefaultContentLoader
{
protected override object LoadContent(Uri uri)
{
var content = base.LoadContent(uri);
if (content == null)
return content;
var vm = Caliburn.Micro.ViewModelLocator.LocateForView(content);
if (vm == null)
return content;
if (content is DependencyObject)
{
Caliburn.Micro.ViewModelBinder.Bind(vm, content as DependencyObject, null);
}
return content;
}
}
For any view model the LocateForView
method every time returns null. I think that problem here is the IoC container which from some reasons is empty in my CaliburnContentLoader.
For big picture here is my App XALM:
<Application x:Class="Astrea.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Astrea">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Bootstrapper x:Key="bootstrapper" />
<local:CaliburnContentLoader x:Key="CaliburnContentLoader" />
</ResourceDictionary>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
and MainWindow xaml
<mui:ModernWindow x:Class="Astrea.Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
Title="mui" IsTitleVisible="True"
ContentLoader="{StaticResource CaliburnContentLoader}"
ContentSource="/Views/ChildView.xaml">
<mui:ModernWindow.MenuLinkGroups>
<mui:LinkGroup DisplayName="nodes explorer">
<mui:LinkGroup.Links>
<mui:Link DisplayName="home" Source="/Views/ChildView.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
Maybe something is wrong in my bootstrapper? Here is code:
namespace Astrea
{
class Bootstrapper : BootstrapperBase
{
private CompositionContainer container;
public Bootstrapper()
{
Initialize();
}
protected override void Configure()
{
// Add New ViewLocator Rule
ViewLocator.NameTransformer.AddRule(
@"(?<nsbefore>([A-Za-z_]\w*\.)*)?(?<nsvm>ViewModels\.)(?<nsafter>([A-Za-z_]\w*\.)*)(?<basename>[A-Za-z_]\w*)(?<suffix>ViewModel$)",
@"${nsbefore}Views.${nsafter}${basename}View",
@"(([A-Za-z_]\w*\.)*)?ViewModels\.([A-Za-z_]\w*\.)*[A-Za-z_]\w*ViewModel$"
);
container = new CompositionContainer(
new AggregateCatalog(
new AssemblyCatalog(typeof(IShellViewModel).Assembly),
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>().FirstOrDefault()
)
);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
}
public T GetInstance<T>()
{
string contract = AttributedModelServices.GetContractName(typeof(T));
var sexports = container.GetExportedValues<object>(contract);
if (sexports.Count() > 0)
return sexports.OfType<T>().First();
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
}
protected override object GetInstance(Type serviceType, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
var exports = container.GetExportedValues<object>(contract);
return exports.FirstOrDefault();
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
//return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
var ret = Enumerable.Empty<object>();
string contract = AttributedModelServices.GetContractName(serviceType);
return container.GetExportedValues<object>(contract);
}
protected override void BuildUp(object instance)
{
container.SatisfyImportsOnce(instance);
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[] {
Assembly.GetExecutingAssembly()
};
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<MainWindowViewModel>();
}
}
}