Return to Snippet

Revision: 34085
at October 17, 2010 02:44 by jiewmeng


Initial Code
interface IDialogService
{
    public string[] GetOpenFileDialog();
    public string GetSaveFileDialog();
}

class DialogService : IDialogService
{
    public string[] GetOpenFileDialog(string title, string filter)
    {
        var dialog = new OpenFileDialog();
        dialog.CheckFileExists = true;
        dialog.CheckPathExists = true;
        dialog.Multiselect = true;
        dialog.Title = title;
        dialog.Filter = filter;
        if ((bool)dialog.ShowDialog()) {
            return dialog.SafeFileNames;
        }
        return new string[0];
    }

    public string GetSaveFileDialog(string title, string filter)
    {
        var dialog = new SaveFileDialog();
        dialog.Title = title;
        dialog.Filter = filter;
        if ((bool)dialog.ShowDialog()) {
            return dialog.SafeFileName;
        }
        return "";
    }
}

Initial URL

                                

Initial Description
In MVVM, where there should be little or no code behind with strong separation of concerns, one might wonder how to create dialogs. If you show a dialog using `new SaveFileDialog()`, when Unit Testing, a dialog will show, halting the unit test. With a service class such as this, one might provide a service implementing `IDialogService` that provides the ViewModel with the required information

Initial Title
Dialog Service for MVVM Apps

Initial Tags

                                

Initial Language
C#