Patrick Quijano
Patrick Quijano

Reputation: 351

How to Show Dialogs Using MVVM Light for WPF?

I need your help. I already have spent researching for the correct way on how to show dialogs using MVVM Light for WPF. However, I'm out of luck.

I read this about on how to implement/use DialogService: https://marcominerva.wordpress.com/2014/10/14/dialogservice-in-mvvm-light-v5/ only to find out that it has no DialogService. I have to implement DialogService for WPF.

Can somebody help me on how to implement DialogService for WPF? Your help is highly appreciated.

Upvotes: 2

Views: 3104

Answers (1)

Patrick Quijano
Patrick Quijano

Reputation: 351

I'm not sure if this is a perfect solution but I created a service to show dialog.

public interface IDialogService {
    void ShowError(Exception Error, string Title);
    void ShowError(string Message, string Title);
    void ShowInfo(string Message, string Title);
    void ShowMessage(string Message, string Title);
    bool ShowQuestion(string Message, string Title);
    void ShowWarning(string Message, string Title);
}

public class DialogService : IDialogService {
    public void ShowError(Exception Error, string Title) {
        MessageBox.Show(Error.ToString(), Title, MessageBoxButton.OK, MessageBoxImage.Error);
    }

    public void ShowError(string Message, string Title) {
        MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
    }

    public void ShowInfo(string Message, string Title) {
        MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Information);
    }

    public void ShowMessage(string Message, string Title) {
        MessageBox.Show(Message, Title, MessageBoxButton.OK);
    }

    public bool ShowQuestion(string Message, string Title) {
        return MessageBox.Show(Message, Title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
    }

    public void ShowWarning(string Message, string Title) {
        MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}

This works fine with me. If there's a need to change on the platform, you can just modify the DialogService class.

Upvotes: 2

Related Questions