B
B
Boris the Animal2015-07-14 11:50:25
WPF
Boris the Animal, 2015-07-14 11:50:25

An exception is thrown in the XAML Designer. Sometimes it crashes, sometimes it doesn't. Details in the topic with pictures. Who faced?

I checked everything in the code that is possible, I don’t even understand what to look for. I use the project template and the MvvmLight library. Designer mode does not run any code that could throw exceptions.
For example:

public class ViewModelLocator : ViewModelLocatorBase
    {
        private readonly IContainer _container;
        private static ViewModelLocator _locator;

        private MainViewModel _designerMainVm;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            _locator = this;

            LogManager.Logger.LogPath = LocalConfiguration.Instance.Folders.Logs;
            CorrelationsLocator.Initialize();

            if (ViewModelBase.IsInDesignModeStatic)
            {
                return;
            }

            Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            Application.Current.MainWindow.Loaded += MainWindow_Loaded;
            Application.Current.MainWindow.Closing += MainWindow_Closing;

            DispatcherHelper.Initialize();

            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);
        }

        /// <summary>
        /// Зарегистрировать зависимости
        /// </summary>
        /// <param name="config">Локальная конфигурация программы</param>
        /// <returns></returns>
        private IContainer RegisterDependencies(LocalConfiguration config)
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<MainViewModel>() //Мнемосхема, логика программы
                .OnActivated(e =>
                {
                    var appManager = e.Context.Resolve<L1ApplicationManagerViewModel>();
                    appManager.Main = e.Instance;

                    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(ApplicationsManager, RecipesEditor, CarsEditor, ClientsEditor);
        }

        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            Debug.WriteLine("Начало: MainWindow_Closing");

            Window window = (Window)sender;
            //Main.OnWindowClosing();

            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 MainViewModel Main
        {
            get
            {
                return !ViewModelBase.IsInDesignModeStatic ?
                    _container.Resolve<MainViewModel>() :
                    _designerMainVm ?? (_designerMainVm =
                    new MainViewModel { Settings = new AppSettings() });
            }
        }
    }

0895b975f1e049f2a1fa06152f0ef380.jpgd79b6b2c30fa40f2b5e18fc615894534.jpg4a234fa6b44a4be99677c0824631ce16.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Larry Underwood, 2015-07-14
@Hydro

Again, you are stepping on the SAME rake, trying to mark up the ENTIRE application in one designer, and even with an IoC container.
Go buy a stick of RAM.
And when the customer's application will be brutally slow down - advise him to buy a more powerful computer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question