Answer the question
In order to leave comments, you need to log in
The XAML designer is simply deadly slow after adding Autofac. Has anyone encountered such a problem?
Since the entire text of the question did not fit, I added another code as an answer in this thread, probably the most important part of it.
I would call it not otefek, but watafak? Nothing really happens in the class constructors that could cause brakes, it didn’t slow down before using Autofac.
The bottom line is that the designer displays approximately the same thing that will be displayed when the application starts, the blocks that I wrapped in
if (!ViewModelBase.IsInDesignModeStatic)
{
}
<Application x:Class="Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
LoadCompleted="App_OnLoadCompleted"
StartupUri="Views\MainWindow.xaml"
d1p1:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/BSU.Controls;component/Resources/Skins/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator xmlns:vm="clr-namespace:Client.ViewModels"
x:Key="Locator"
d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
</Application>
<Window.DataContext>
<Binding Path="Main"
Source="{StaticResource Locator}" />
</Window.DataContext>
Answer the question
In order to leave comments, you need to log in
Updated: 07/10/2015
In general, I solved the problem like this. In the ViewModelLocator constructor, I do almost nothing in the code designer mode, and I made the property like this:
public MainViewModel Main
{
get
{
return !ViewModelBase.IsInDesignModeStatic ?
_container.Resolve<MainViewModel>() :
_designerMainVm ?? (_designerMainVm =
new MainViewModel(null, null, null, null) { Settings = new AppSettings() });
}
}
public class ViewModelLocator : ViewModelLocatorBase
{
private readonly IContainer _container;
private static ViewModelLocator _locator;
public ViewModelLocator()
{
_locator = this;
if (!ViewModelBase.IsInDesignModeStatic)
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
Application.Current.MainWindow.Loaded += MainWindow_Loaded;
Application.Current.MainWindow.Closing += MainWindow_Closing;
}
LogManager.Logger.LogPath = LocalConfiguration.Instance.Folders.Logs;
DispatcherHelper.Initialize();
CorrelationsLocator.Initialize();
if (!ViewModelBase.IsInDesignModeStatic)
{
ProgramIdentification.Initialize(ProgramIdentifier.CbpOperator);
LocalConfiguration.Instance.Folders.CheckFolders();
LocalConfiguration.Instance.LoadAppConfig();
LocalConfiguration.Instance.LoadLocalSettings();
WpfSingleInstance.AppIsAlreadyRunning += (sender, args) =>
MessageBox.Show(string.Format("Приложение: {0} уже запущено", args.AppName));
WpfSingleInstance.Make();
}
_container = RegisterDependencies(LocalConfiguration.Instance);
}
private IContainer RegisterDependencies(LocalConfiguration config)
{
var builder = new ContainerBuilder();
builder.RegisterType<LauncherViewModel>().SingleInstance();
builder.RegisterType<ShutdownViewModel>().SingleInstance();
builder.RegisterType<CommandManager>().SingleInstance();
builder.RegisterType<AccountControllerVm>().SingleInstance();
builder.RegisterType<ComponentEditorViewModel>().SingleInstance();
builder.RegisterType<ComponentSelectorViewModel>().SingleInstance();
builder.RegisterType<RecipeTimeSetEditorViewModel>().SingleInstance();
builder.RegisterType<CategoryEditorViewModel>().SingleInstance();
builder.RegisterType<AboutDialogViewModel>().SingleInstance();
builder.RegisterType<L1ApplicationManagerViewModel>().SingleInstance();
builder.RegisterType<CarEditorViewModel>() //Редактор машин
.OnActivated(e =>
{
e.Instance.WindowSettings = config.Settings.CarsWindowSettings; //Настройки окна (положение, размер, состояние)
}).SingleInstance();
builder.RegisterType<ClientsEditorViewModel>() //Редактор клиентов
.OnActivated(e =>
{
e.Instance.WindowSettings = config.Settings.ClientsWindowSettings; //Настройки окна (положение, размер, состояние)
}).SingleInstance();
builder.RegisterType<ReportsWindowViewModel>() //Отчёты
.OnActivated(e =>
{
e.Instance.WindowSettings = config.Settings.ReportsWindowSettings; //Настройки окна (положение, размер, состояние)
e.Instance.ReportsFolder = config.Folders.Documents;
e.Instance.ReportMode = ReportMode.Applications;
});
builder.RegisterType<EventLogViewModel>() //Журнал событий
.OnActivated(e =>
{
e.Instance.WindowSettings = config.Settings.EventLogWindowSettings; //Настройки окна (положение, размер, состояние)
e.Instance.SetConfiguration(config.Config, config.Folders.EventReportFolder);
});
builder.RegisterType<RecipesEditorViewModel>() //Редактор рецептов
.OnActivated(e =>
{
e.Instance.WindowSettings = config.Settings.RecipeWindowSettings; //Настройки окна (положение, размер, состояние)
e.Instance.SetEditorSettings(config.Settings.RecipeEditorConfig);
})
.SingleInstance();
builder.RegisterType<ApplicationsEditorViewModel>() //Редактор заявок
.OnActivated(e =>
{
e.Instance.LastSelectedItems = config.Settings.LastSelectedItemsInAppWindow; //Состояние последних выбранных элементов.
e.Instance.WindowSettings = config.Settings.AppEditorWindowSettings;
})
.SingleInstance();
builder.RegisterType<MainViewModel>()
.OnActivated(e =>
{
var appManager = e.Context.Resolve<L1ApplicationManagerViewModel>();
appManager.Main = e.Instance;
if (!ViewModelBase.IsInDesignModeStatic)
{
e.Instance.Title = ProgramIdentification.GetCurrentProgramName();
}
e.Instance.Settings = config.Settings.ApplicationSettings;
})
.SingleInstance();
return builder.Build();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
WindowManager.Instance.Initialize(Application.Current.MainWindow, Main);
Main.OnWindowLoaded();
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
Debug.WriteLine("Начало: MainWindow_Closing");
Window window = (Window)sender;
Debug.WriteLine("Конец: MainWindow_Closing");
if (WindowManager.Instance.OpenMessageDialog(DialogIcons.Help, DialogButtons.YesNo,
"Выход из программы",
"Вы действительно хотите завершить работу приложения?") == UserDialogResult.No)
e.Cancel = true;
else
{
window.Closing -= MainWindow_Closing;
try
{
LocalConfiguration.Instance.SaveLocalSettings();
LocalConfiguration.Instance.SaveCurrentAppConfig();
}
catch (Exception ex)
{
LogManager.Logger.AppendException(ex);
}
}
}
private void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogManager.Logger.AppendException(e.Exception);
MessageBox.Show(e.Exception.ToString());
}
#region Свойства
public static ViewModelLocator Current
{
get { return _locator; }
}
public MainViewModel Main
{
get { return _container.Resolve<MainViewModel>(); }
}
public L1ApplicationManagerViewModel ApplicationsManager
{
get { return _container.Resolve<L1ApplicationManagerViewModel>(); }
}
public AccountControllerVm AccountController
{
get { return _container.Resolve<AccountControllerVm>(); }
}
public ShutdownViewModel Shutdown
{
get { return _container.Resolve<ShutdownViewModel>(); }
}
public CategoryEditorViewModel CategoryEditor
{
get { return _container.Resolve<CategoryEditorViewModel>(); }
}
public ApplicationsEditorViewModel ApplicationsEditor
{
get { return _container.Resolve<ApplicationsEditorViewModel>(); }
}
public RecipesEditorViewModel RecipesEditor
{
get { return _container.Resolve<RecipesEditorViewModel>(); }
}
public ComponentEditorViewModel ComponentEditor
{
get { return _container.Resolve<ComponentEditorViewModel>(); }
}
public ComponentSelectorViewModel ComponentsSelector
{
get { return _container.Resolve<ComponentSelectorViewModel>(); }
}
/// <summary>Редактор клиентов</summary>
public ClientsEditorViewModel ClientsEditor
{
get { return _container.Resolve<ClientsEditorViewModel>(); }
}
public CarEditorViewModel CarsEditor
{
get { return _container.Resolve<CarEditorViewModel>(); }
}
public CommandManager CommonCommands
{
get { return _container.Resolve<CommandManager>(); }
}
public LauncherViewModel Launcher
{
get { return _container.Resolve<LauncherViewModel>(); }
}
public AboutDialogViewModel About
{
get { return _container.Resolve<AboutDialogViewModel>(); }
}
/// <summary>
/// Возвращает новый экземпляр модели представления Отчётов
/// </summary>
public ReportsWindowViewModel NewReport
{
get { return _container.Resolve<ReportsWindowViewModel>(); }
}
/// <summary>
/// Возвращает новый экземпляр модели представления Журнала событий
/// </summary>
public EventLogViewModel NewEventLog
{
get { return _container.Resolve<EventLogViewModel>(); }
}
#endregion
#region Методы
#endregion
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question