/ Published in: C#
                    
                                        
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
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
interface IDialogService
{
public string[] GetOpenFileDialog();
public string GetSaveFileDialog();
}
class DialogService : IDialogService
{
public string[] GetOpenFileDialog(string title, string filter)
{
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Multiselect = true;
dialog.Title = title;
dialog.Filter = filter;
if ((bool)dialog.ShowDialog()) {
return dialog.SafeFileNames;
}
}
public string GetSaveFileDialog(string title, string filter)
{
dialog.Title = title;
dialog.Filter = filter;
if ((bool)dialog.ShowDialog()) {
return dialog.SafeFileName;
}
return "";
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                