【问题标题】:WPF using SaveFileDialog in MVVMWPF 在 MVVM 中使用 SaveFileDialog
【发布时间】:2018-05-01 12:44:30
【问题描述】:

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

我正在使用 RelayCommand 类并启动 SaveAsFileCommand。在 SaveAsFileCommand 中,我使用 lambda 表达式将两个参数拆分为:

RichTextBox 控件实例和目标路径(filePath

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

要在视图层中创建 SaveDialogBox,我使用了 3 个类: DialogBoxFileDialogBoxSaveFileDialogBox

在 XAML 中,我创建了 SaveDialogBox 并尝试使用 MultiBinding 调用 SaveAsFileCommand 以传递这两个命令参数。

为了显示 SaveDialogBox,我使用了绑定到 SaveDialogBox

的按钮

问题是:在这个地方,编译器抱怨它不能为我的 SaveDialogBox 执行多重绑定,因为它是非 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>

SaveAsFileCommand:

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);

        }
    }

RelayCommand 类:

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();
    }
}

【问题讨论】:

  • 我假设您希望自己实现对话框?因为如果您对内置的系统对话框感到满意并且仍然对 MVVM 友好,那么总有 MvvmDialogs 供您使用。
  • 在这种情况下,我需要实现内置对话框(正是来自 Microsoft.Win32 命名空间的对话框),但我想从 View 层而不是从 ViewModel 或 Model 调用它们 - 这会导致问题跨度>

标签: wpf file mvvm dialog save


【解决方案1】:

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

请在此查看确认请求者:

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

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

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

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多