/// <summary> /// フィルタパターンリスト文字列からファイルオープンダイアログパラメータのFiltersを設定する。 /// </summary> /// <param name="self">設定対象のパラメータ</param> /// <param name="filtersText">フィルタパターンリスト文字列</param> public static void SetFiltersFrom(this ShellOpenFileDialogParameter self, string filtersText) { setFilters(self.Filters, filtersText); }
/// <summary> /// パスリスト文字列からファイルオープンダイアログパラメータのAdditionalPlacesを設定する。 /// </summary> /// <param name="self">設定対象のパラメータ</param> /// <param name="placesText">パスリスト文字列</param> public static void SetAdditionalPlaces(this ShellOpenFileDialogParameter self, string placesText) { setPlaces(self.AdditionalPlaces, placesText); }
// 構築 #region コンストラクタ /// <summary> /// デフォルトコンストラクタ /// </summary> public OpenFileDialogViewModel() { // ダイアログ表示用インタラクションヘルパ var showInteraction = new ReactiveInteraction <ShellOpenFileDialogActionParameter>(); // ダイアログ表示用インタラクションのトリガソース this.ShowDialogRequest = showInteraction.Source; // ビューのエラー状態 this.HasViewError = new ReactivePropertySlim <bool>() .AddTo(this.Disposables); // ダイアログ結果設定用デリゲート var dlgResultFilter = default(Action <uint?>); var dlgResultItems = default(Action <string>); // ダイアログ表示コマンド this.ShowDialogCommand = new[] { this.HasViewError.Inverse(), } .CombineLatestValuesAreAllTrue() .ToReactiveCommand() .WithSubscribe(() => showOpenDialog()) .AddTo(this.Disposables); // デフォルト設定参照用 var defParam = new ShellOpenFileDialogParameter(); #region 動作設定:状態設定 this.Directory = new ReactivePropertySlim <string>(defParam.Directory) .AddTo(this.Disposables); this.InitialFileName = new ReactivePropertySlim <string>(defParam.InitialFileName) .AddTo(this.Disposables); this.DefaultExtension = new ReactivePropertySlim <string>(defParam.DefaultExtension) .AddTo(this.Disposables); this.Filters = new ReactivePropertySlim <string>(string.Join("\n", new[] { "画像ファイル|*.png;*.jpg;*.gif;*.bmp", "テキストファイル|*.txt;*.text;", "全てのファイル|*.*", })) .AddTo(this.Disposables); this.InitialFilterIndex = new ReactivePropertySlim <uint>(defParam.InitialFilterIndex) .AddTo(this.Disposables); this.DefaultDirectory = new ReactivePropertySlim <string>(defParam.DefaultDirectory) .AddTo(this.Disposables); this.ClientGuid = new ReactivePropertySlim <Guid>(defParam.ClientGuid) .AddTo(this.Disposables); #endregion #region 動作設定:オプション this.StrictFileTypes = new ReactivePropertySlim <bool>(defParam.StrictFileTypes) .AddTo(this.Disposables); this.NoChangeDirectory = new ReactivePropertySlim <bool>(defParam.NoChangeDirectory) .AddTo(this.Disposables); this.PickFolders = new ReactivePropertySlim <bool>(defParam.PickFolders) .AddTo(this.Disposables); this.ForceFileSystem = new ReactivePropertySlim <bool>(defParam.ForceFileSystem) .AddTo(this.Disposables); this.AllNonStorageItems = new ReactivePropertySlim <bool>(defParam.AllNonStorageItems) .AddTo(this.Disposables); this.NoValidate = new ReactivePropertySlim <bool>(defParam.NoValidate) .AddTo(this.Disposables); this.AllowMultiSelect = new ReactivePropertySlim <bool>(defParam.AllowMultiSelect) .AddTo(this.Disposables); this.PathMustExist = new ReactivePropertySlim <bool>(defParam.PathMustExist) .AddTo(this.Disposables); this.FileMustExist = new ReactivePropertySlim <bool>(defParam.FileMustExist) .AddTo(this.Disposables); this.CreatePrompt = new ReactivePropertySlim <bool>(defParam.CreatePrompt) .AddTo(this.Disposables); this.ShareAware = new ReactivePropertySlim <bool>(defParam.ShareAware) .AddTo(this.Disposables); this.NoReadOnlyReturn = new ReactivePropertySlim <bool>(defParam.NoReadOnlyReturn) .AddTo(this.Disposables); this.NoTestFileCreate = new ReactivePropertySlim <bool>(defParam.NoTestFileCreate) .AddTo(this.Disposables); this.HidePinnedPlaces = new ReactivePropertySlim <bool>(defParam.HidePinnedPlaces) .AddTo(this.Disposables); this.NoDereferenceLinks = new ReactivePropertySlim <bool>(defParam.NoDereferenceLinks) .AddTo(this.Disposables); this.OkButtonNeedsInteraction = new ReactivePropertySlim <bool>(defParam.OkButtonNeedsInteraction) .AddTo(this.Disposables); this.DontAddToRecent = new ReactivePropertySlim <bool>(defParam.DontAddToRecent) .AddTo(this.Disposables); this.ForceShowHidden = new ReactivePropertySlim <bool>(defParam.ForceShowHidden) .AddTo(this.Disposables); this.ForcePreviewPaneOn = new ReactivePropertySlim <bool>(defParam.ForcePreviewPaneOn) .AddTo(this.Disposables); #endregion #region 動作設定:ダイアログカスタマイズ this.Title = new ReactivePropertySlim <string>(defParam.Title) .AddTo(this.Disposables); this.AcceptButtonLabel = new ReactivePropertySlim <string>(defParam.AcceptButtonLabel) .AddTo(this.Disposables); this.FileNameLabel = new ReactivePropertySlim <string>(defParam.FileNameLabel) .AddTo(this.Disposables); this.AdditionalPlaces = new ReactivePropertySlim <string>(string.Join("\n", new[] { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), })) .AddTo(this.Disposables); #endregion #region ダイアログ結果 this.ResultFilterIndex = Observable.FromEvent <uint?>(h => dlgResultFilter += h, h => dlgResultFilter -= h) .ToReadOnlyReactivePropertySlim() .AddTo(this.Disposables); this.ResultItems = Observable.FromEvent <string>(h => dlgResultItems += h, h => dlgResultItems -= h) .ToReadOnlyReactivePropertySlim() .AddTo(this.Disposables); #endregion #region ローカル関数 // ファイルオープンダイアログ表示ローカル関数 void showOpenDialog() { // ダイアログパラメータを準備 var dlgParam = new ShellOpenFileDialogParameter(); dlgParam.Directory = this.Directory.ValueIfNotEmpty(); dlgParam.InitialFileName = this.InitialFileName.ValueIfNotEmpty(); dlgParam.DefaultExtension = this.DefaultExtension.ValueIfNotEmpty(); dlgParam.SetFiltersFrom(this.Filters.Value); dlgParam.InitialFilterIndex = this.InitialFilterIndex.Value; dlgParam.DefaultDirectory = this.DefaultDirectory.ValueIfNotEmpty(); dlgParam.ClientGuid = this.ClientGuid.Value; dlgParam.StrictFileTypes = this.StrictFileTypes.Value; dlgParam.NoChangeDirectory = this.NoChangeDirectory.Value; dlgParam.PickFolders = this.PickFolders.Value; dlgParam.ForceFileSystem = this.ForceFileSystem.Value; dlgParam.AllNonStorageItems = this.AllNonStorageItems.Value; dlgParam.NoValidate = this.NoValidate.Value; dlgParam.AllowMultiSelect = this.AllowMultiSelect.Value; dlgParam.PathMustExist = this.PathMustExist.Value; dlgParam.FileMustExist = this.FileMustExist.Value; dlgParam.CreatePrompt = this.CreatePrompt.Value; dlgParam.ShareAware = this.ShareAware.Value; dlgParam.NoReadOnlyReturn = this.NoReadOnlyReturn.Value; dlgParam.NoTestFileCreate = this.NoTestFileCreate.Value; dlgParam.HidePinnedPlaces = this.HidePinnedPlaces.Value; dlgParam.NoDereferenceLinks = this.NoDereferenceLinks.Value; dlgParam.OkButtonNeedsInteraction = this.OkButtonNeedsInteraction.Value; dlgParam.DontAddToRecent = this.DontAddToRecent.Value; dlgParam.ForceShowHidden = this.ForceShowHidden.Value; dlgParam.ForcePreviewPaneOn = this.ForcePreviewPaneOn.Value; dlgParam.Title = this.Title.ValueIfNotEmpty(); dlgParam.AcceptButtonLabel = this.AcceptButtonLabel.ValueIfNotEmpty(); dlgParam.FileNameLabel = this.FileNameLabel.ValueIfNotEmpty(); dlgParam.SetAdditionalPlaces(this.AdditionalPlaces.Value); // ダイアログ表示インタラクションパラメータを準備 var actionParam = new ShellOpenFileDialogActionParameter(); actionParam.Parameter = dlgParam; // ダイアログ表示要求を発行 showInteraction.Raise(actionParam); // 結果をプロパティに設定 if (actionParam.Exception != null) { // なんらか例外が生じた dlgResultItems?.Invoke(actionParam.Exception.Message); dlgResultFilter?.Invoke(null); } else if (actionParam.Result != null) { // ダイアログ結果。表示してキャンセルした場合を含む。 var items = string.Join("\n", actionParam.Result.Items); if (string.IsNullOrWhiteSpace(items)) { items = "<cancelled>"; } dlgResultItems?.Invoke(items); dlgResultFilter?.Invoke(actionParam.Result.FilterIndex); } else { // 通常あり得ない状態。 dlgResultItems?.Invoke("<no result>"); dlgResultFilter?.Invoke(null); } } #endregion }