WPF 在 MVVM 中使用 SaveFileDialog

标签 wpf file mvvm dialog save

我正在努力在 MVVM 中使用 SaveFileDialog。

我正在使用 RelayCommand 类并启动 另存为文件命令 .在 SaveAsFileCommand 中,使用 lambda 表达式我将两个参数拆分为:

的实例富文本框 控制和目标路径 ( filePath )

然后我使用上述参数调用 DataIO.SaveAsFile(arguments[0], arguments[1])。

创建 保存对话框 在 View 层中,我使用了 3 个类:
对话框 , 文件对话框保存文件对话框

在 XAML 中,我创建了 SaveDialogBox 并尝试调用 另存为文件命令 使用 多重绑定(bind) 传递这两个命令参数。

显示保存对话框 我使用绑定(bind)到 的按钮保存对话框

问题是 :在这个地方,编译器提示它不能为我的 SaveDialogBox 执行多重绑定(bind),因为它是非 DependencyObject 和非 DependencyProperty。
如何解决该问题并使用 DialogBox 正确保存文件在我的情况下符合MVVM???

XAML 部分代码:

<Button Command="{Binding ElementName=SaveFileDB, Path=Show}" > 
    <Button.ToolTip>
        <ToolTip Style="{StaticResource styleToolTip}" >
            <TextBlock Text="Save" Style="{StaticResource styleTextBlockTP}" />
        </ToolTip>
    </Button.ToolTip>
    <Image Source="Icon\Save.png"/>
</Button>


<local:SaveFileDialogBox x:Name="SaveFileDB" Caption="Save content to file..."
                             Filter="Text files (*.txt)|*.txt|All files(*.*)|*.*"
                             FilterIndex="0" DefaultExt="txt"
                             CommandFileOK="{Binding SaveAsFileCommand}" >
        <local:SaveFileDialogBox.CommandParemeter>
            <MultiBinding Converter="{StaticResource parametersConverter}">
                <Binding ElementName="MainRichTextBox" />
                <Binding ElementName="SaveFileDB" Path="Show" />
            </MultiBinding>
        </local:SaveFileDialogBox.CommandParemeter>
    </local:SaveFileDialogBox>

另存为文件命令:
private ICommand _SaveAsFileCommand;
public ICommand SaveAsFileCommand
{
    get
    {
        if (_SaveAsFileCommand == null)
        {
            _SaveAsFileCommand = new RelayCommand(
            argument =>
                {
                    var arguments = (object[])argument;
                    DataIO.SaveAsFile(arguments[0], arguments[1]); 
                }
                );
        }
        return _SaveAsFileCommand;
    }
}

DataIO.SaveAsFile 方法:
    public static void SaveAsFile(object argument0, object argument1)
    {
        System.Windows.Controls.RichTextBox richTB = argument0 as System.Windows.Controls.RichTextBox;
        string path = (string)argument1;


        using (FileStream fileStream = new FileStream(path, FileMode.Create))
        {
            TextRange textRange = new TextRange(richTB.Document.ContentStart, richTB.Document.ContentEnd);
            textRange.Save(fileStream, DataFormats.Text);

        }
    }

中继命令类:
class RelayCommand : ICommand
{
    private readonly Action<object> _Execute;
    private readonly Func<object, bool> _CanExecute;

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute)
    {
        if (execute == null) throw new ArgumentNullException("execute");
        _Execute = execute;
        _CanExecute = canExecute;
    }

    public RelayCommand(Action<object> execute)
    {
        if (execute == null) throw new ArgumentNullException("execute");
        _Execute = execute;
    }

    public bool CanExecute(object parameter)
    {
        return _CanExecute == null ? true : _CanExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_CanExecute != null) CommandManager.RequerySuggested += value;
        }
        remove
        {
            if (_CanExecute != null) CommandManager.RequerySuggested -= value;
        }
    }

    public void Execute(object parameter)
    {
        _Execute(parameter);
    }
}

对话框类:
public abstract class DialogBox : FrameworkElement, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string parameter)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(parameter));
    }

    protected Action<object> execute = null;

    public string Caption { get; set; }

    protected ICommand show;
    public virtual ICommand Show
    {
        get
        {
            if (show == null)
                show = new RelayCommand(execute);
            return show;
        }
    }
}

FileDialogBox 类:
public abstract class FileDialogBox : CommandDialogBox
{
    public bool? FileDialogResult { get; protected set; }
    public string FilePath { get; set; }
    public string Filter { get; set; }
    public int FilterIndex { get; set; }
    public string DefaultExt { get; set; }

    protected Microsoft.Win32.FileDialog fileDialog = null;

    protected FileDialogBox()
    {
        execute =
            o =>
            {
                var values = (object[])o;
                RelayCommand relCom1 = (RelayCommand)values[1];

                fileDialog.Title = Caption;
                fileDialog.Filter = Filter;
                fileDialog.FilterIndex = FilterIndex;
                fileDialog.DefaultExt = DefaultExt;

                string filePath = "";

                if (FilePath != null) filePath = FilePath; else FilePath = "";
                //if (o != null) filePath = (string)o;
                //if (o != null) filePath = (string)values[1];
                if (o != null) filePath = relCom1.ToString();
                if (!String.IsNullOrWhiteSpace(filePath))
                {
                    fileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(filePath);
                    fileDialog.FileName = System.IO.Path.GetFileName(filePath);
                }

                FileDialogResult = fileDialog.ShowDialog();
                OnPropertyChanged("FileDialogResult");
                if (FileDialogResult.HasValue && FileDialogResult != null)
                {
                    FilePath = fileDialog.FileName;
                    OnPropertyChanged("FilePath");
                    ExecuteCommand(CommandFileOK, FilePath);
                };
            };
    }

    public static DependencyProperty CommandFileOKProperty =
        DependencyProperty.Register("CommandFileOK", typeof(ICommand), typeof(FileDialogBox));

    public ICommand CommandFileOK
    {
        get { return (ICommand)GetValue(CommandFileOKProperty); }
        set { SetValue(CommandFileOKProperty, value); }
    }
}

SaveFileDialogBox 类:
public class SaveFileDialogBox : FileDialogBox
{
    public SaveFileDialogBox()
    {
        fileDialog = new SaveFileDialog();
    }
}

最佳答案

我在对话框中处理用户输入要求的方式是使用进入 View 但没有 UI 的控件。
我将要完成的命令分成两部分。
本质上,这些是在您完成时显示对话框并调用命令。
该控件显示一个对话框,该对话框获取数据,然后调用您通过绑定(bind)提供的命令。
由于这是控件,因此您可以很好地绑定(bind)它并且它位于可视树中,因此它可以获得对窗口的引用。

请参阅此中的确认请求者:

https://gallery.technet.microsoft.com/WPF-User-Notification-MVVM-98940828

这是为了确认删除记录,但同样的原则可以扩展到文件选择器,只需稍作修改。

由此,一旦用户单击并关闭对话框,调用的命令就可以捕获您需要的任何变量。如果你绑定(bind)它们:

private RelayCommand _confirmCommand;
public RelayCommand ConfirmCommand
{
    get
    {
        return _confirmCommand
          ?? (_confirmCommand = new RelayCommand(
               () =>
               {
                   confirmer.Caption = "Please Confirm";
                   confirmer.Message = "Are you SURE you want to delete this record?";
                   confirmer.MsgBoxButton = MessageBoxButton.YesNo;
                   confirmer.MsgBoxImage = MessageBoxImage.Warning;
                   OkCommand = new RelayCommand(
                       () =>
                       {
                           // You would do some actual deletion or something here
                           UserNotificationMessage msg = new UserNotificationMessage { Message = "OK.\rDeleted it.\rYour data is consigned to oblivion.", SecondsToShow = 5 };
                           Messenger.Default.Send<UserNotificationMessage>(msg);
                       });
                   RaisePropertyChanged("OkCommand");
                   ShowConfirmation = true;
               }));
    }
}

从确认请求者,调用该命令:
    public static readonly DependencyProperty ShowConfirmDialogProperty =
        DependencyProperty.Register("ShowConfirmDialog",
                    typeof(bool?),
                    typeof(ConfirmationRequestor),
                    new FrameworkPropertyMetadata(null
                        , new PropertyChangedCallback(ConfirmDialogChanged)
                        )
                    { BindsTwoWayByDefault = true }
                    );
    private static void ConfirmDialogChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool?)e.NewValue != true)
        {
            return;
        }
        ConfirmationRequestor cr = (ConfirmationRequestor)d;
        Window parent = Window.GetWindow(cr) as Window;
        MessageBoxResult result = MessageBox.Show(parent, cr.Message, cr.Caption, cr.MsgBoxButton, cr.MsgBoxImage);
        if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
        {
            if (cr.Command != null)
            {
                cr.Command.Execute(cr.CommandParameter);
            }
        }
        cr.SetValue(ShowConfirmDialogProperty, false);
    }

关于WPF 在 MVVM 中使用 SaveFileDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50116713/

相关文章:

c# - WPF 组合框作为 System.Windows.Media.Colors

file - 如何使用 Rust 创建二进制文件?

Python将C头文件转换为dict

c++ - 如何在不使用系统复制命令的情况下将可执行文件的二进制代码复制到新文件中?

wpf - 键盘焦点的样式触发器

WPF HierarchicalDataTemplate 不会在属性更改时更新 ItemsSource

c# - 在后台线程中创建 wpf 控件

c# - Caliburn.Micro GetAllInstances 只返回一个 viewModel(Caliburn.Micro MVVM)

wpf ContentPresenter 不会启用/禁用

wpf - 为什么 WPF 中的 TextBox.Text 不可设置动画?