Skip to content

Instantly share code, notes, and snippets.

@igorkulman
Last active August 29, 2015 14:12
Show Gist options
  • Save igorkulman/fda1860b35d5312e9157 to your computer and use it in GitHub Desktop.
Save igorkulman/fda1860b35d5312e9157 to your computer and use it in GitHub Desktop.
/// <summary>
/// Helper class for showing message dialogs
/// </summary>
public static class DialogHelper
{
/// <summary>
/// Shows a dialog with given message and ok/cancel buttons.
/// </summary>
/// <param name="message">Message</param>
/// <param name="title">Title</param>
/// <param name="okText">OK text (optional)</param>
/// <param name="cancelText">Cancel text (optional)</param>
/// <returns>True if ok pressed, false otherwise</returns>
public static async Task<bool> ShowMessageDialog(string message, string title, string okText, string cancelText)
{
bool result = false;
var dialog = new MessageDialog(message, title);
if (!string.IsNullOrWhiteSpace(okText))
{
dialog.Commands.Add(new UICommand(okText, cmd => result = true));
}
if (!string.IsNullOrWhiteSpace(cancelText))
{
dialog.Commands.Add(new UICommand(cancelText, cmd => result = false));
}
await dialog.ShowAsync();
return result;
}
}
/// <summary>
/// Helper class for showing message dialogs
/// </summary>
public class DialogHelperService
{
/// <summary>
/// Shows a dialog with given message and ok/cancel buttons.
/// </summary>
/// <param name="message">Message</param>
/// <param name="title">Title</param>
/// <param name="okText">OK text (optional)</param>
/// <param name="cancelText">Cancel text (optional)</param>
/// <returns>True if ok pressed, false otherwise</returns>
public async Task<bool> ShowMessageDialog(string message, string title, string okText, string cancelText)
{
bool result = false;
var dialog = new MessageDialog(message, title);
if (!string.IsNullOrWhiteSpace(okText))
{
dialog.Commands.Add(new UICommand(okText, cmd => result = true));
}
if (!string.IsNullOrWhiteSpace(cancelText))
{
dialog.Commands.Add(new UICommand(cancelText, cmd => result = false));
}
await dialog.ShowAsync();
return result;
}
}
public async void Message()
{
var res = await DialogHelper.ShowMessageDialog("Do you really want to do this?","My Title","Hell yeah!","No way");
if (res)
{
Result = "Hell yeah!";
} else
{
Result = "NOOOO!";
}
}
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Accept" Label="go ask!" x:Name="Message" />
</CommandBar>
</Page.BottomAppBar>
public async void Message()
{
var res = await _dialogHelperService.ShowMessageDialog("Do you really want to do this?","My Title","Hell yeah!","No way");
if (res)
{
Result = "Hell yeah!";
} else
{
Result = "NOOOO!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment