在IDialogService方法的基础上:
那么对于System.Windows.MessageBoxResult
枚举类型呢?是不是最好在接口中不涉及该枚举类型,只在实现中使用它?
我选择的处理方式是:
我在IDialogInterface旁添加了一个枚举,它包含四个选项:是,否,确定和取消。
namespace Foo.Bar.Dialogs
{
public enum DialogResult { Ok, Yes, No, Cancel }
public interface IDialogService
{
void ShowErrorBox(string error_message);
DialogResult ShowQuestionBox(string question_message);
DialogResult ShowQuestionBox(string question_message, string caption);
DialogResult ShowQuestionBox(string question_message, string caption, bool allow_cancel);
DialogResult ShowQuestionBox(string question_message, string caption, bool allow_cancel, bool show_as_error);
void ShowWarningBox(string message, string caption = "");
void ShowInformationBox(string message);
void ShowInformationBox(string message, string caption);
}
}
初始问题:
我正在将所有命令从我的.asmx.cs文件移动到某个应用程序主窗口的ViewModel中。
现在,我必须弄清楚如何处理需要用户确认的命令。
暂时,我会在我的ViewModel中吸收必要的类型,以直接启动我的对话框。我很确定这不是最好或最干净的方法。
我发现这篇文章使用了一个IDialogService接口,提供了一种有趣而且更为简洁的方法。
public interface IDialogService
{
int Width { get; set; }
int Height { get; set; }
void Show(string title, string message, Action<DialogResult> onClosedCallback);
}
我还发现了这篇文章,它似乎更好,因为它在尝试使用IDialogInterface之前会检查其是否为空:
private void PerformAddNewCustomer()
{
CustomerList.Add(new Customer { Name = "Name" + i });
i++;
if (dialogService != null)
{
dialogService.Show("Customed added");
}
}
这是将对话框与ViewModel分离的最佳方法吗?还是有更好的方法?