Display Message Box and File Open/ Save Dialog Box in MVVM design pattern based application




MVVM design pattern discourages code behind and introduces command based processing. It comes with whole host of advantages but makes tricky to display message box or even file open/save dialog box. This blog post introduces classes that can be used for above said purpose either from ViewModel or Model as per the design of your application.




These classes are part of Saral Framework that I have hosted recently on CodePlex. The vision behind Saral framework is to put together the entire infrastructure required to perform most common tasks while working with MVVM based Silverlight/WPF application.




I have just posted the initial version of this Framework which provides infrastructure to display ShowMessage(), OpenFileDialog() and SaveFileDialog() from Model or ViewModel layer of MVVM based Silverlight/WPF application.


namespace Saral.ShowMessage
{
    /// <summary>
    /// Interface is designed to present basic messages from Model or ViewModel layer while implemeting MVVM Design Pattern
    /// </summary>
    /// <remarks>
    /// Interface convers only the basic message display scenarios.    
    /// </remarks>
    public interface IMessageDisplay
    {         
        /// <summary>
        /// Presents message
        /// </summary>        
        /// <param name="message">message to be displayed</param>
        void ShowMessage(string message);

        /// <summary>
        /// Presents warning message
        /// </summary>        
        /// <param name="message">message to be displayed</param>
        void ShowWarning(string message);

        /// <summary>
        /// Presents error message
        /// </summary>
        /// <param name="message">message to be displayed</param>
        void ShowError(string message);

    }
}







namespace Saral.ShowXFileDialog
{
    /// <summary>
    /// This service allows a user to specify a filename to open or save a file.
    /// </summary>
    /// <remarks>
    /// This interface is designed for simplicity. If you have to accomplish more advanced
    /// scenarios then we recommend implementing your own specific message service.
    /// </remarks>
    public interface IFileDialogDisplay
    {

        /// <summary>
        /// Shows the open file dialog box that allows a user to specify a file that should be opened.
        /// </summary>
        /// <param name="fileTypes">The supported file types.</param>
        /// <param name="defaultFileType">Default file type.</param>
        /// <param name="defaultFileName">Default filename.</param>
        /// <returns>A FileDialogResult object which contains the filename selected by the user.</returns>
        /// <exception cref="ArgumentNullException">fileTypes must not be null.</exception>
        /// <exception cref="ArgumentException">fileTypes must contain at least one item.</exception>
        FileDialogResult ShowOpenFileDialog(IEnumerable<FileType> fileTypes, FileType defaultFileType, string defaultFileName);

        /// <summary>
        /// Shows the save file dialog box that allows a user to specify a filename to save a file as.
        /// </summary>
        /// <param name="fileTypes">The supported file types.</param>
        /// <param name="defaultFileType">Default file type.</param>
        /// <param name="defaultFileName">Default filename.</param>
        /// <returns>A FileDialogResult object which contains the filename entered by the user.</returns>
        /// <exception cref="ArgumentNullException">fileTypes must not be null.</exception>
        /// <exception cref="ArgumentException">fileTypes must contain at least one item.</exception>
        FileDialogResult ShowSaveFileDialog(IEnumerable<FileType> fileTypes, FileType defaultFileType, string defaultFileName);


    }
}



 





Download link: Source code






I will post an example application in next blog post.


No comments: