Guest User

Untitled

a guest
May 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <Page
  2. x:Class="DuckTracker.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:DuckTracker"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  11. <Grid.RowDefinitions>
  12. <RowDefinition></RowDefinition>
  13. <RowDefinition></RowDefinition>
  14. <RowDefinition></RowDefinition>
  15. </Grid.RowDefinitions>
  16.  
  17. <Grid.ColumnDefinitions>
  18. <ColumnDefinition></ColumnDefinition>
  19. <ColumnDefinition></ColumnDefinition>
  20. </Grid.ColumnDefinitions>
  21.  
  22. <TextBlock Grid.Row="0" Grid.Column="0" Height="20">Name:</TextBlock>
  23. <TextBox Grid.Row="0" Grid.Column="1" Height="20"></TextBox>
  24. <Button Click="Button_Click" Grid.Row="2" VerticalAlignment="Bottom">Login</Button>
  25. </Grid>
  26. </Page>
  27.  
  28. using System;
  29. using System.Threading.Tasks;
  30. using Windows.UI.Xaml;
  31. using Windows.UI.Xaml.Controls;
  32.  
  33. namespace DuckTracker
  34. {
  35.  
  36. public sealed partial class MainPage : Page
  37. {
  38. public MainPage()
  39. {
  40. this.InitializeComponent();
  41. }
  42.  
  43. private async void Button_Click(object sender, RoutedEventArgs e)
  44. {
  45. bool canLogin = await CanLogin();
  46.  
  47. if (canLogin == false)
  48. {
  49. try
  50. {
  51. await AlertWithMessages("Fail", "Could not log in!", "ok");
  52.  
  53. }
  54. catch (Exception ex)
  55. {
  56.  
  57. var dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
  58. await dialog.ShowAsync();
  59. }
  60.  
  61. }
  62. }
  63.  
  64. public async Task AlertWithMessages(string title, string msg, string confirm)
  65. {
  66. ContentDialog dialog = new ContentDialog()
  67. {
  68. Title = title,
  69. Content = msg,
  70. PrimaryButtonText = confirm
  71. };
  72.  
  73. await ContentDialogMaker.CreateContentDialogAsync(dialog, true);
  74.  
  75. }
  76.  
  77. public async Task<bool> CanLogin()
  78. {
  79. await Task.Delay(1000);
  80.  
  81. return false;
  82. }
  83. }
  84. }
  85.  
  86. using System;
  87. using System.Threading.Tasks;
  88. using Windows.UI.Xaml.Controls;
  89.  
  90. namespace DuckTracker
  91. {
  92. public static class ContentDialogMaker
  93. {
  94. public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
  95. public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
  96.  
  97. static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
  98. {
  99. if (ActiveDialog != null)
  100. {
  101. if (awaitPreviousDialog)
  102. {
  103. await DialogAwaiter.Task;
  104. DialogAwaiter = new TaskCompletionSource<bool>();
  105. }
  106. else ActiveDialog.Hide();
  107. }
  108. ActiveDialog = Dialog;
  109. ActiveDialog.Closed += ActiveDialog_Closed;
  110. await ActiveDialog.ShowAsync();
  111. ActiveDialog.Closed -= ActiveDialog_Closed;
  112. }
  113.  
  114. public static ContentDialog ActiveDialog;
  115. static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
  116. private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
  117. }
  118. }
  119.  
  120. private async void Button_Click(object sender, RoutedEventArgs e)
  121. {
  122. if (DoingStuff == false)
  123. {
  124. DoingStuff = true;
  125.  
  126. bool canLogin = await CanLogin();
  127.  
  128. if (canLogin == false)
  129. {
  130. try
  131. {
  132.  
  133. await AlertWithMessages("Fail", "Could not log in!", "ok");
  134.  
  135. }
  136. catch (Exception ex)
  137. {
  138.  
  139. var dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
  140. await dialog.ShowAsync();
  141. }
  142.  
  143. }
  144.  
  145. DoingStuff = false;
  146. }
  147. }
Add Comment
Please, Sign In to add comment