以下是我的问题:
用户点击“保存”按钮
出现“另存为”对话框,用于选择目标路径
执行“保存”按钮的点击事件的RelayCommand
目前我对以下内容一无所知:
如何打开对话框&使用EventToCommand绑定执行RelayCommand
如何将选中的“另存为”对话框路径传递给RelayCommand
我正在使用MVVM Light库。
答案 0 :(得分:2)
虽然我认为Dmitriy Reznik的答案相当不错,但另一种解决方案是使用按钮的命令来完成ViewModel中的大部分工作。这并不严格遵循MVVM模式,但可能更容易实现。
将Button上的Command设置为ViewModel上的ICommand。 ICommand启动SaveFileDialog,并在对话框关闭后将文件写入磁盘。由于您使用的是MVVM Light,我将使用RelayCommand来实现ICommand。
的Xaml:
<Button Command="{Binding SaveAsClickCmd}/>
代码:
public class MyViewModel
{
public RelayCommand SaveAsClickCmd
{
get {
return _saveAsClickCmd ?? (_saveAsClickCmd = new RelayCommand(() => {
var dialog = new Microsoft.Win32.SaveFileDialog();
if (dialog.ShowDialog() != true)
return;
using (var stream = dialog.OpenFile()) {
//write out file to disk
}
}));
}
}
private RelayCommand _saveAsClickCmd;
}
答案 1 :(得分:1)
我已经基于MVVM Light库中的DialogMessage类创建了一个类(您需要引用MVVM Light库)
public class SaveFileDialogMessage : GenericMessage<string>
{
/// <summary>
/// Initializes a new instance of the <see cref="SaveFileDialogMessage" /> class.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="filter">The filter.</param>
/// <param name="callback">The callback.</param>
public SaveFileDialogMessage(string content, string filter, Action<bool?, string> callback)
: base(content)
{
Filter = filter;
Callback = callback;
}
/// <summary>
/// Initializes a new instance of the <see cref="SaveFileDialogMessage" /> class.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="content">The content.</param>
/// <param name="filter">The filter.</param>
/// <param name="callback">The callback.</param>
public SaveFileDialogMessage(object sender, string content, string filter, Action<bool?, string> callback)
: base(sender, content)
{
Filter = filter;
Callback = callback;
}
/// <summary>
/// Initializes a new instance of the <see cref="SaveFileDialogMessage" /> class.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="target">The target.</param>
/// <param name="content">The content.</param>
/// <param name="filter">The filter.</param>
/// <param name="callback">The callback.</param>
public SaveFileDialogMessage(object sender, object target, string content, string filter, Action<bool?, string> callback)
: base(sender, target, content)
{
Filter = filter;
Callback = callback;
}
/// <summary>
/// Gets a callback method that should be executed to deliver the result
/// of the message box to the object that sent the message.
/// </summary>
public Action<bool?, string> Callback { get; private set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; set; }
/// <summary>
/// Sets or gets the filter property.
/// </summary>
public string Filter { get; set; }
/// <summary>
/// Utility method, checks if the <see cref="Callback" /> property is
/// null, and if it is not null, executes it.
/// </summary>
/// <param name="result">The result that must be passed
/// to the dialog message caller.</param>
/// <param name="fileName">Name of the file.</param>
public void ProcessCallback(bool? result, string fileName)
{
if (Callback != null)
{
Callback(result, fileName);
}
}
然后在你的ViewModel中你有这样的东西:
var dialog = new SaveFileDialogMessage("Title", "XML Files" + "|" + ".xml", ProcessSaveFileDialog);
Messenger.Default.Send(dialog);
private void ProcessSaveFileDialog(bool? dialogResult, string fileName)
{
..........
}
并在您的View构造函数中:
/// <summary>
/// Initialize a new instance of the <see cref="MainView"/> class.
/// </summary>
public MainView()
{
InitializeComponent();
Messenger.Default.Register<SaveFileDialogMessage>(this, msg =>
{
var sfd = new SaveFileDialog { Filter = msg.Filter, Title = msg.Title };
var result = sfd.ShowDialog();
msg.ProcessCallback(result, sfd.FileName);
});
}
答案 2 :(得分:0)
就个人而言,我不会通过在viewModel中完成所有操作来追逐MVVM纯度,并且会为代码隐藏节省一些代码。组件不是开箱即用的MVVM并不是那么不寻常,而且“追逐”可能需要时间,而客户会失去价值。
无论如何,对于当前的问题,有这种方法:
通过这种方式,您可以正确地从表示层分离viewModel(不提供选择表单的详细信息),同时仍然提供所需的功能。
另一种方法是定义支持MVVM的自定义openFileDialog。您可以将当前viewModel传递给对话框,对话框将更新viewmodel上的selectedPath属性。
更好的是,您可以在codeBehing中执行此操作,然后使用selectedPath属性在viewModel上调用某个方法。这样可以省去所有麻烦。
答案 3 :(得分:0)
我知道这是一个老线程,但对于初学者(像我一样),获取正确的示例非常重要。
就个人而言,我不喜欢上面的任何一个例子,因为我觉得在ViewModel中声明一个对话框违反了MVVM模式。恕我直言,ViewModel必须不知道任何与UI相关的控件,如对话框或消息框。
谷歌搜索时,我发现了这个:http://www.matt.digital/mvvm-light-communicating-across-layers-with-services/恕我直言,这是一个如何以类似MVVM的方式呈现对话框的完美例子。