Exemplo n.º 1
0
 internal static void DestroyCommand()
 {
     manageInstallationsView?.Dispose();
     manageInstallationsView = null;
     manageInstallationsViewModel?.Dispose();
     manageInstallationsViewModel = null;
 }
Exemplo n.º 2
0
 internal static void DestroyCommand()
 {
     imagingToolsView?.Dispose();
     imagingToolsView = null;
     imagingToolsViewModel?.Dispose();
     imagingToolsViewModel = null;
 }
Exemplo n.º 3
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();

            MainWindow = mainWindow;

            try
            {
                if (File.Exists(Environment.CurrentDirectory + "\\" + "soundboardData.xml"))
                {
                    MainWindow.DataContext = _mainViewModel = vm.XmlSerializationService.Deserialize <vm.MainViewModel>("soundboardData.xml");
                }
                else
                {
                    MainWindow.DataContext = _mainViewModel = new vm.MainViewModel();
                }
            }
            catch (Exception ex)
            {
                var result = vm.AppServices.MessageBoxService.ShowMessageBoxDecision("Some problem occurred when application was starting to run. Do you want to see the details?",
                                                                                     "Problem with startup.", vm.MessageBoxButton.YesNo, vm.MessageBoxImage.Error);

                if (result == vm.MessageBoxResult.Yes)
                {
                    vm.AppServices.MessageBoxService.ShowMessageBox(ex.Message, "Error details", vm.MessageBoxImage.Information);
                }

                _mainViewModel?.Dispose();

                MainWindow.DataContext = _mainViewModel = new vm.MainViewModel();
            }

            mainWindow.Show();
        }
Exemplo n.º 4
0
        public void VmShouldDisposeIocContainerIfSettingsTrue()
        {
            IViewModel viewModel = GetViewModelBase();

            viewModel.Settings.DisposeIocContainer = true;
            viewModel.Dispose();
            ((IocContainerMock)viewModel.IocContainer).IsDisposed.ShouldBeTrue();
        }
Exemplo n.º 5
0
        public void VmShouldNotDisposeIocContainerIfSettingsFalse()
        {
            IViewModel viewModel = GetViewModelBase();

            viewModel.Settings.DisposeIocContainer = false;
            viewModel.Dispose();
            ((IocContainerMock)viewModel.IocContainer).IsDisposed.ShouldBeFalse();
        }
 private void PopViewFromStack(IViewModel model)
 {
     foreach (var pair in stack.SkipWhile(p => p.Key != model).Reverse().ToList())
     {
         stack.Remove(pair.Key);
         UnsubscribeFrom(pair.Value);
         model.Dispose();
     }
 }
Exemplo n.º 7
0
        public void VmShouldThrowsExceptionDisposed()
        {
            IViewModel viewModel = GetViewModelBase();

            viewModel.Dispose();
            viewModel.IsDisposed.ShouldBeTrue();

            ShouldThrow <ObjectDisposedException>(() => viewModel.InitializeViewModel(DataContext.Empty));
        }
Exemplo n.º 8
0
        private void BindLifeTimeActions(IViewModel model)
        {
            //bind model lifetime to local
            var modelLifeTime = model.LifeTime;

            modelLifeTime.ComposeCleanUp(_viewModelLifeTime, Close);

            _viewModelLifeTime.AddCleanUpAction(() =>
            {
                if (_isViewOwner)
                {
                    model.Dispose();
                }
            });

            _viewModelLifeTime.AddCleanUpAction(_progressLifeTime.Terminate);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Clears the navigation stack and deactivates all view models.
        /// </summary>
        public async Task ClearNavigationStackAsync()
        {
            // Cylces over all views and view models that are on the navigation stack
            while (this.navigationStack.Any())
            {
                // Gets the view and the view model
                IViewModel viewModel = this.navigationStack.Pop();

                // Deactivates and disposes of the view model
                if (viewModel != null)
                {
                    await viewModel.OnDeactivateAsync();

                    viewModel.Dispose();
                }
            }
        }
Exemplo n.º 10
0
        public void PopToRoot()
        {
            for (int i = Items.Count - 1; i > 0; i--)
            {
                IViewModel viewModel = Items[i];
                viewModel.Dispose();

                Items.RemoveAt(i);

                if (ViewModelPopped != null)
                {
                    ViewModelPopped(this, viewModel);
                }
            }

            ActiveItem = Items.Single();
            ActiveItem.OnActivated();
        }
Exemplo n.º 11
0
        private IViewModel CreateView <T>(MainWindowViewModel mainWindowViewModel, IViewModel viewModel) where T : UserControl, new()
        {
            if (viewModel != null)
            {
                viewModel.OnClosing(false);
                viewModel.Dispose();
            }

            View = new T();

            var dataContext = View.DataContext as IViewModel;

            if (dataContext != null)
            {
                dataContext.MainWindow = mainWindowViewModel;
            }

            return(View.DataContext as IViewModel);
        }
Exemplo n.º 12
0
        public void Pop(bool clear = false)
        {
            if (Items.Count() == 1 && !clear)
            {
                return;
            }
            if (!Items.Any())
            {
                return;
            }
            IViewModel viewModel = Items.Last();

            viewModel.Dispose();

            Items.Remove(viewModel);

            if (Items.Any())
            {
                ActiveItem = Items.Last();
            }
            else
            {
                ActiveItem = null;
            }

            if (ActiveItem != null)
            {
                ActiveItem.OnActivated();
            }

            if (ViewModelPopped != null)
            {
                ViewModelPopped(this, viewModel);
            }

            if (!Items.Any())
            {
                OnCloseRequested();
            }
        }
Exemplo n.º 13
0
 protected override void OnClosing(CancelEventArgs e)
 {
     vm.OnDeactivate();
     vm.Dispose();
     base.OnClosing(e);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Navigates the user to the specified view.
        /// </summary>
        /// <param name="parameters">The parameters that are to be passed to the view model.</param>
        /// <typeparam name="TView">The type of the view to which the user is to be navigated.</typeparam>
        /// <exception cref="InvalidOperationException">If the view or the view model can not be instantiated, or the window does not support navigation, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns <see cref="NavigationResult.Navigated"/> if the user was successfully navigated and <see cref="NavigationResult.Canceled"/> otherwise.</returns>
        public async Task <NavigationResult> NavigateAsync <TView>(object parameters) where TView : Page
        {
            // Raises the on navigated from event of the current view model, if the view model does not allow to be navigated away from, then the navigation is aborted
            NavigationEventArgs eventArguments = null;

            if (this.CurrentViewModel != null)
            {
                eventArguments = new NavigationEventArgs(NavigationReason.Navigation);
                await this.CurrentViewModel.OnNavigateFromAsync(eventArguments);

                if (eventArguments.Cancel)
                {
                    return(NavigationResult.Canceled);
                }
            }

            // Determines the type of the view model, which can be done via attribute or convention
            Type viewModelType = null;
            ViewModelAttribute viewModelAttribute = typeof(TView).GetTypeInfo().GetCustomAttributes <ViewModelAttribute>().FirstOrDefault();

            if (viewModelAttribute != null)
            {
                viewModelType = viewModelAttribute.ViewModelType;
            }
            else
            {
                this.assemblyTypes = this.assemblyTypes ?? typeof(TView).GetTypeInfo().Assembly.GetTypes();
                string viewModelName = this.ViewModelNamingConvention(typeof(TView).Name);
                viewModelType = this.assemblyTypes.FirstOrDefault(type => type.Name == viewModelName);
            }

            // Instantiates the new view model
            IViewModel viewModel = null;

            if (viewModelType != null)
            {
                try
                {
                    // Lets the IOC container instantiate the view model, so that all dependencies can be injected (including the navigation service itself, which is set as an explitic constructor argument)
                    viewModel = this.iocContainer.GetInstance(viewModelType, this).Inject(parameters) as IViewModel;
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("The view model could not be instantiated.", e);
                }

                // Checks whether the view model implements the IViewModel interface
                if (viewModel == null)
                {
                    throw new InvalidOperationException("The type of the view model must be an implementation of IViewModel.");
                }
            }

            // Calls the activate event and then the navigate event of the view model
            if (viewModel != null)
            {
                // Raises the activate event of the new view model
                await viewModel.OnActivateAsync();

                // Raises the on navigate to event of the new view model, and checks if it allows to be navigated to
                eventArguments = new NavigationEventArgs(NavigationReason.Navigation);
                await viewModel.OnNavigateToAsync(eventArguments);

                if (eventArguments.Cancel)
                {
                    // Since the view model does not allow to be navigated to, the new view model is deactivated, disposed of, and the navigation is aborted
                    await viewModel.OnDeactivateAsync();

                    viewModel.Dispose();
                    return(NavigationResult.Canceled);
                }
            }

            // Adds the old view to the navigation stack
            if (this.CurrentViewModel != null)
            {
                this.CurrentViewModel.IsInView = false;
            }
            if (this.CurrentView != null)
            {
                this.navigationStack.Push(this.CurrentViewModel);
            }

            // Instantiates the new view
            TaskCompletionSource <Page> taskCompletionSource  = new TaskCompletionSource <Page>();
            NavigatedEventHandler       navigatedEventHandler = (sender, e) => taskCompletionSource.SetResult(e.Content as Page);

            this.navigationFrame.Navigated += navigatedEventHandler;
            this.navigationFrame.Navigate(typeof(TView));
            this.CurrentView = await taskCompletionSource.Task;
            this.navigationFrame.Navigated -= navigatedEventHandler;

            // Sets the view model as data context of the view and sets the new current view model
            this.CurrentView.DataContext = viewModel;
            this.CurrentViewModel        = viewModel;

            // Sets an indicator in the new view model, that it is now in view
            if (this.CurrentViewModel != null)
            {
                this.CurrentViewModel.IsInView = true;
            }

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(NavigationResult.Navigated);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Navigates the user to the specified view.
        /// </summary>
        /// <param name="parameters">The parameters that are to be passed to the view model.</param>
        /// <typeparam name="TView">The type of the view to which the user is to be navigated.</typeparam>
        /// <exception cref="InvalidOperationException">If the view or the view model can not be instantiated, or the window does not support navigation, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns <see cref="NavigationResult.Navigated"/> if the user was successfully navigated and <see cref="NavigationResult.Canceled"/> otherwise.</returns>
        public async Task <NavigationResult> NavigateAsync <TView>(object parameters) where TView : Page
        {
            // Checks if the current window supports navigation
            if (!this.SupportsNavigation)
            {
                throw new InvalidOperationException(Resources.Localization.NavigationService.NavigationNotSupportedExceptionMessage);
            }

            // Raises the on navigated from event of the current view model, if the view model does not allow to be navigated away from, then the navigation is aborted
            NavigationEventArgs eventArguments = null;

            if (this.CurrentViewModel != null)
            {
                eventArguments = new NavigationEventArgs(NavigationReason.Navigation);
                await this.CurrentViewModel.OnNavigateFromAsync(eventArguments);

                if (eventArguments.Cancel)
                {
                    return(NavigationResult.Canceled);
                }
            }

            // Determines the type of the view model, which can be done via attribute or convention
            Type viewModelType = null;
            ViewModelAttribute viewModelAttribute = typeof(TView).GetCustomAttributes <ViewModelAttribute>().FirstOrDefault();

            if (viewModelAttribute != null)
            {
                viewModelType = viewModelAttribute.ViewModelType;
            }
            else
            {
                this.assemblyTypes = this.assemblyTypes ?? typeof(TView).Assembly.GetTypes();
                string viewModelName = this.ViewModelNamingConvention(typeof(TView).Name);
                viewModelType = this.assemblyTypes.FirstOrDefault(type => type.Name == viewModelName);
            }

            // Instantiates the new view model
            IViewModel viewModel = null;

            if (viewModelType != null)
            {
                try
                {
                    // Lets the IOC container instantiate the view model, so that all dependencies can be injected (including the navigation service itself, which is set as an explitic constructor argument)
                    viewModel = this.iocContainer.GetInstance(viewModelType, this).Inject(parameters) as IViewModel;
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException(Resources.Localization.NavigationService.ViewModelCouldNotBeInstantiatedExceptionMessage, e);
                }

                // Checks whether the view model implements the IViewModel interface
                if (viewModel == null)
                {
                    throw new InvalidOperationException(Resources.Localization.NavigationService.WrongViewModelTypeExceptionMessage);
                }
            }

            // Calls the activate event and then the navigate event of the view model
            if (viewModel != null)
            {
                // Raises the activate event of the new view model
                await viewModel.OnActivateAsync();

                // Raises the on navigate to event of the new view model, and checks if it allows to be navigated to
                eventArguments = new NavigationEventArgs(NavigationReason.Navigation);
                await viewModel.OnNavigateToAsync(eventArguments);

                if (eventArguments.Cancel)
                {
                    // Since the view model does not allow to be navigated to, the new view model is deactivated, disposed of, and the navigation is aborted
                    await viewModel.OnDeactivateAsync();

                    viewModel.Dispose();
                    return(NavigationResult.Canceled);
                }
            }

            // Instantiates the new view
            Page view = null;

            try
            {
                // Lets the IOC container instantiate the view, so that all dependencies can be injected
                view = this.iocContainer.GetInstance <TView>();
            }
            catch (Exception e)
            {
                // Since an error occurred, the new window view model is deactivated and disposed of
                if (viewModel != null)
                {
                    await viewModel.OnDeactivateAsync();

                    viewModel.Dispose();
                }

                // Rethrows the exception
                throw new InvalidOperationException(Resources.Localization.NavigationService.ViewCouldNotBeInstantiatedExceptionMessage, e);
            }

            // Since view is a framework element it must be properly initialized
            if (!view.IsInitialized)
            {
                MethodInfo initializeComponentMethod = view.GetType().GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance);
                if (initializeComponentMethod != null)
                {
                    initializeComponentMethod.Invoke(view, new object[0]);
                }
            }

            // Sets the view model as data context of the view
            view.DataContext = viewModel;

            // Adds the old view to the navigation stack
            if (this.CurrentViewModel != null)
            {
                this.CurrentViewModel.IsInView = false;
            }
            if (this.CurrentView != null)
            {
                this.navigationStack.Push(new KeyValuePair <Page, IViewModel>(this.CurrentView, this.CurrentViewModel));
            }

            // Sets the current view and view model
            this.CurrentView      = view;
            this.CurrentViewModel = viewModel;

            // Navigates the user to the new view
            this.navigationFrame.Navigate(this.CurrentView);
            if (this.CurrentViewModel != null)
            {
                this.CurrentViewModel.IsInView = true;
            }

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(NavigationResult.Navigated);
        }
Exemplo n.º 16
0
 public void CloseDetached(IViewModel viewModel)
 {
     viewModel.Dispose();
     DetachedItems.Remove(viewModel);
 }
Exemplo n.º 17
0
 protected override void Dispose(bool disposing)
 {
     Stop();
     _viewModel?.Dispose();
     base.Dispose(disposing);
 }