Filtration/Filtration/App.xaml.cs

92 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.Linq;
2015-06-04 13:15:54 -04:00
using System.Windows;
using System.Windows.Threading;
2015-06-26 12:42:20 -04:00
using AutoMapper;
using Castle.Facilities.TypedFactory;
2015-06-04 13:15:54 -04:00
using Castle.MicroKernel.ModelBuilder.Inspectors;
using Castle.Windsor;
using Castle.Windsor.Installer;
using Filtration.ObjectModel;
2015-07-01 17:28:29 -04:00
using Filtration.ObjectModel.ThemeEditor;
using Filtration.Properties;
2015-07-01 17:28:29 -04:00
using Filtration.ThemeEditor.ViewModels;
using Filtration.ViewModels;
2015-06-04 13:15:54 -04:00
using Filtration.Views;
2015-07-03 17:16:17 -04:00
using NLog;
2015-06-04 13:15:54 -04:00
namespace Filtration
{
2015-07-15 14:36:42 -04:00
public partial class App : Application
2015-06-04 13:15:54 -04:00
{
private IWindsorContainer _container;
2015-07-03 17:16:17 -04:00
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
2015-06-04 13:15:54 -04:00
private void Application_Startup(object sender, StartupEventArgs e)
{
DispatcherUnhandledException += OnDispatcherUnhandledException;
2015-06-04 13:15:54 -04:00
_container = new WindsorContainer();
var propInjector = _container.Kernel.ComponentModelBuilder
.Contributors
.OfType<PropertiesDependenciesModelInspector>()
.Single();
2015-06-04 13:15:54 -04:00
_container.Kernel.ComponentModelBuilder.RemoveContributor(propInjector);
2015-06-26 12:42:20 -04:00
_container.AddFacility<TypedFactoryFacility>();
_container.Install(FromAssembly.InThisApplication());
2015-06-26 12:42:20 -04:00
Mapper.Configuration.ConstructServicesUsing(_container.Resolve);
Mapper.CreateMap<ItemFilterBlockGroup, ItemFilterBlockGroupViewModel>()
.ForMember(destination => destination.ChildGroups, options => options.ResolveUsing(
delegate(ResolutionResult resolutionResult)
{
var context = resolutionResult.Context;
var showAdvanced = (bool) context.Options.Items["showAdvanced"];
var group = (ItemFilterBlockGroup) context.SourceValue;
if (showAdvanced)
return group.ChildGroups;
else
return group.ChildGroups.Where(c => c.Advanced == false);
}))
.ForMember(dest => dest.SourceBlockGroup,
opts => opts.MapFrom(from => from))
.ForMember(dest => dest.IsExpanded,
opts => opts.UseValue(false));
2015-07-01 17:28:29 -04:00
Mapper.CreateMap<Theme, IThemeEditorViewModel>().ConstructUsingServiceLocator();
2015-07-01 17:28:29 -04:00
Mapper.CreateMap<ThemeComponent, ThemeComponentViewModel>().ReverseMap();
Mapper.CreateMap<IThemeEditorViewModel, Theme>();
2015-07-01 17:28:29 -04:00
2015-06-26 12:42:20 -04:00
Mapper.AssertConfigurationIsValid();
2015-06-04 13:15:54 -04:00
var mainWindow = _container.Resolve<IMainWindow>();
mainWindow.Show();
}
public void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
2015-07-03 17:16:17 -04:00
_logger.Fatal(e.Exception);
var exception = e.Exception.Message + Environment.NewLine + e.Exception.StackTrace;
var innerException = e.Exception.InnerException != null
? e.Exception.InnerException.Message + Environment.NewLine +
e.Exception.InnerException.StackTrace
: string.Empty;
MessageBox.Show(
exception + Environment.NewLine + innerException,
"Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
2015-06-04 13:15:54 -04:00
protected override void OnExit(ExitEventArgs e)
{
_container.Dispose();
Settings.Default.Save();
2015-06-04 13:15:54 -04:00
base.OnExit(e);
}
}
}