Command setup for AlertMessageService

Jul 11, 2013 at 1:13 PM
Hi,

Do you have any examples of custom buttons for the AlertMessageService (IEnumerable<DialogCommand> dialogCommands).

Need a delete confirmation popup and i am struggling to implement it in the prism app.

regards

Chris
Editor
Jul 11, 2013 at 6:19 PM
Edited Jul 11, 2013 at 6:20 PM
Hi,

As it can be seen in the solution of the AdventureWorksShopper RI, the AlertMessageService is a simple service that creates a MessageDialog and adds the dialogCommands passed as UICommands to the message. In the following code snippet you can see how the AlertMessageService uses them to create the corresponding UICommands:
// From the ShowAsync method of the AlertMessageService
[...]
    var commands = dialogCommands.Select(c => new UICommand(c.Label, (command) => c.Invoked(), c.Id));
    foreach (var command in commands)
    {
        messageDialog.Commands.Add(command);
    }
[...]
Also, I believe you might find the following links useful to understand how the MessageDialogs works:
I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Jul 13, 2013 at 6:55 PM
Hi Damian

Thankyou for the information. How do i go about setting up the Ienumerable containing the button commands that are passed into alertMessageService call ie.

//setup IEnumerable<DialogCommand> here specifying the buttons required here

await _alertMessageService.ShowAsync("Confirm Delete", "Confirmation", "pass in the button settings here ");


which is passed to the class

public async Task ShowAsync(string message, string title, IEnumerable<DialogCommand> dialogCommands)
{
//code here
}


thanks for your time

chris
Editor
Jul 15, 2013 at 8:48 PM
Edited Jul 15, 2013 at 8:49 PM
Hi Chris,

In the following code snippet you can find a simple example where the AlertMessageService is used to change the background color of a Button using three DialogCommands:
public Brush BackgroundColor { get; set; }

public void RaiseAlert()
{
    DialogCommand blueCommand = new DialogCommand() { Id = 1, Label = "Blue", Invoked = () => this.ChangeColor(Colors.Blue) };
    DialogCommand redCommand = new DialogCommand() { Id = 2, Label = "Red", Invoked = () => this.ChangeColor(Colors.Red) };
    DialogCommand blackCommand = new DialogCommand() { Id = 3, Label = "Black", Invoked = () => this.ChangeColor(Colors.Black) };

    IList<DialogCommand> commands = new List<DialogCommand>();
    commands.Add(blueCommand);
    commands.Add(redCommand);
    commands.Add(blackCommand);

    this._alertService.ShowAsync("Please select the background color for the button.", "Background Color", commands);
}

public void ChangeColor(Color color)
{
    this.BackgroundColor = new SolidColorBrush(color);
    this.OnPropertyChanged("BackgroundColor");
}
When the RaiseAlert method is invoked an alert message is shown thanks to the AlertMessageService, showing the three Buttons defined above. Upon clicking one of those buttons the alert message is automatically closed and the ChangeColor method is invoked.

I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Jul 15, 2013 at 11:15 PM

Hi Damian

Perfect, that was exactly what I was struggling to implement. I have it working now.

Thanks for the info.

Kind regards

Chris