/// <summary>
            /// show as an asynchronous operation.
            /// </summary>
            /// <param name="d">The d.</param>
            /// <param name="parent">The parent.</param>
            /// <param name="options">The options.</param>
            /// <returns>System.String[].</returns>
            private async Task <string[]> ShowAsync(SystemDialog d, Window parent, ManagedFileDialogOptions options = null)
            {
                var model = new ManagedDialogViewModel((FileSystemDialog)d,
                                                       options ?? new ManagedFileDialogOptions());

                var langService  = DIResolver.Get <ILanguagesService>();
                var language     = langService.GetSelected();
                var fontResolver = DIResolver.Get <IFontFamilyManager>();
                var font         = fontResolver.ResolveFontFamily(language.Font);

                var dialog = new T
                {
                    Icon                  = StaticResources.GetAppIcon(),
                    Content               = new ManagedDialog(),
                    Title                 = d.Title,
                    DataContext           = model,
                    SizeToContent         = SizeToContent.Width,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    FontFamily            = font.GetFontFamily(),
                    Height                = 700
                };

                dialog.Closed += delegate { model.Cancel(); };

                string[] result = null;

                model.CompleteRequested += items =>
                {
                    result = items;
                    dialog.Close();
                };

                model.CancelRequested += dialog.Close;

                await dialog.ShowDialog <object>(parent);

                return(result);
            }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ManagedDialogViewModel" /> class.
        /// </summary>
        /// <param name="dialog">The dialog.</param>
        /// <param name="options">The options.</param>
        /// <exception cref="ArgumentException">dialog</exception>
        public ManagedDialogViewModel(FileSystemDialog dialog, ManagedFileDialogOptions options)
        {
            _options     = options;
            _disposables = new CompositeDisposable();

            var quickSources = AvaloniaLocator.Current
                               .GetService <ManagedDialogSources>()
                               ?? new ManagedDialogSources();

            var sub1 = AvaloniaLocator.Current
                       .GetService <IMountedVolumeInfoProvider>()
                       .Listen(ManagedDialogSources.MountedVolumes);

            var sub2 = Observable.FromEventPattern(ManagedDialogSources.MountedVolumes,
                                                   nameof(ManagedDialogSources.MountedVolumes.CollectionChanged))
                       .ObserveOn(AvaloniaScheduler.Instance)
                       .Subscribe(x => RefreshQuickLinks(quickSources));

            _disposables.Add(sub1);
            _disposables.Add(sub2);

            CompleteRequested += delegate { _disposables?.Dispose(); };
            CancelRequested   += delegate { _disposables?.Dispose(); };

            RefreshQuickLinks(quickSources);

            Title = dialog.Title ?? (
                dialog is OpenFileDialog ? "Open file"
                        : dialog is SaveFileDialog ? "Save file"
                        : dialog is OpenFolderDialog ? "Select directory"
                        : throw new ArgumentException(nameof(dialog)));

            var directory = dialog.Directory;

            if (directory == null || !Directory.Exists(directory))
            {
                directory = Directory.GetCurrentDirectory();
            }

            if (dialog is FileDialog fd)
            {
                if (fd.Filters?.Count > 0)
                {
                    Filters.AddRange(fd.Filters.Select(f => new ManagedDialogFilterViewModel(f)));
                    _selectedFilter = Filters[0];
                    ShowFilters     = true;
                }

                if (dialog is OpenFileDialog ofd)
                {
                    if (ofd.AllowMultiple)
                    {
                        SelectionMode = SelectionMode.Multiple;
                    }
                    FileName = ofd.InitialFileName;
                }
            }

            SelectingFolder = dialog is OpenFolderDialog;

            if (dialog is SaveFileDialog sfd)
            {
                _savingFile       = true;
                _defaultExtension = sfd.DefaultExtension;
                FileName          = sfd.InitialFileName;
            }

            Navigate(directory, (dialog as FileDialog)?.InitialFileName);
            SelectedItems.CollectionChanged += OnSelectionChangedAsync;
        }