Esempio n. 1
0
        public void CloseFile(OpenFileViewModel file)
        {
            int index = OpenFiles.IndexOf(file);

            Debug.Assert(index >= 0);
            file.Dispose();
            OpenFiles.RemoveAt(index);
            if (--index < 0)
            {
                index = 0;
            }
            if (OpenFiles.Count > 0)
            {
                SelectedFile = OpenFiles[index];
            }
        }
        /// <summary>
        /// Closes the file
        /// </summary>
        /// <param name="File">File to close</param>
        public virtual void CloseFile(FileViewModel file)
        {
            if (file != null)
            {
                var args = new FileClosingEventArgs();
                args.File = file;

                FileClosing?.Invoke(this, args);

                if (!args.Cancel)
                {
                    for (var i = OpenFiles.Count - 1; i >= 0; i--)
                    {
                        if (ReferenceEquals(OpenFiles[i], file))
                        {
                            OpenFiles[i].Dispose();

                            var openFilesCount = OpenFiles.Count;
                            try
                            {
                                OpenFiles.RemoveAt(i);
                            }
                            catch (NullReferenceException)
                            {
                                // There's been a bug in a dependency where OpenFiles.RemoveAt fails because of UI stuff.
                                // If the same exception is encountered, and the file has actually been removed, then ignore it.
                                if (openFilesCount <= OpenFiles.Count) // OpenFiles.Count should have decreased by 1. If it has not, then throw the exception.
                                {
                                    throw;
                                }
                            }
                        }
                    }

                    if (ReferenceEquals(file, SelectedFile))
                    {
                        SelectedFile = null;
                    }

                    FileClosed?.Invoke(this, new FileClosedEventArgs {
                        File = file.Model
                    });
                }
            }
        }