Guest User

Untitled

a guest
Feb 15th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. public static MobileServiceClient MobileService = new
  2. MobileServiceClient("URL");
  3.  
  4. private IMobileServiceSyncTable<User_Cred> todoGetTable =
  5. App.MobileService.GetSyncTable<User_Cred>();
  6.  
  7.  
  8.  
  9. private async Task InitLocalStoreAsync()
  10. {
  11. if (!App.MobileService.SyncContext.IsInitialized)
  12. {
  13. var store = new MobileServiceSQLiteStore("Tale-DB");
  14. store.DefineTable<User_Cred>();
  15. await App.MobileService.SyncContext.InitializeAsync(store);
  16. }
  17. await SyncAsync();
  18. }
  19.  
  20. private async Task SyncAsync()
  21. {
  22. await App.MobileService.SyncContext.PushAsync();
  23. await todoGetTable.PullAsync("User_Cred",
  24. todoGetTable.CreateQuery());
  25. }
  26.  
  27.  
  28.  
  29. async public void submitAuthBtn_Click(object sender, RoutedEventArgs e)
  30. {
  31. await InitLocalStoreAsync();
  32.  
  33. GetAuthentication();
  34.  
  35. }
  36.  
  37. async public void GetAuthentication()
  38. {
  39. try
  40. {
  41.  
  42. //IMobileServiceTable<User_Cred> todoTable =
  43. App.MobileService.GetTable<User_Cred>();
  44.  
  45. List<User_Cred> items = await todoGetTable
  46. .Where(User_Cred => User_Cred.Username ==
  47. textBoxUsername.Text)
  48. .ToListAsync();
  49.  
  50. IsAuth = items.Count();
  51.  
  52.  
  53.  
  54. // Return a List UI control value back to the form
  55.  
  56. foreach (var value in items)
  57. {
  58. var dialog = new MessageDialog("Welcome Back " +
  59. value.Username);
  60. await dialog.ShowAsync();
  61. this.Frame.Navigate(typeof(home));
  62. }
  63.  
  64.  
  65. if (IsAuth > 0)
  66. {
  67. var dialog = new MessageDialog("You are Authenticated");
  68. await dialog.ShowAsync();
  69.  
  70.  
  71. }
  72. else
  73. {
  74. var dialog = new MessageDialog(" Account Does Not Exist,
  75. please Register to get Started.");
  76. await dialog.ShowAsync();
  77. }
  78. }
  79. catch (Exception em)
  80. {
  81. var dialog = new MessageDialog("An Error Occured: " +
  82. em.Message);
  83. await dialog.ShowAsync();
  84. }
  85. }
  86.  
  87. async private void submitAuthBtn_Copy_Click(object sender,
  88. RoutedEventArgs e)
  89. {
  90. try
  91. {
  92. User_Cred itemReg = new User_Cred
  93. {
  94. Username = textBoxUsername.Text,
  95. Password = textBoxPassword.Text
  96.  
  97. };
  98. await App.MobileService.GetTable<User_Cred>
  99. ().InsertAsync(itemReg);
  100. var dialog = new MessageDialog("Thank you for Registering! Now
  101. just hit log in");
  102. await dialog.ShowAsync();
  103.  
  104. }
  105. catch (Exception em)
  106. {
  107. var dialog = new MessageDialog("An Error Occured: User Name
  108. Already exists.");
  109.  
  110. await dialog.ShowAsync();
  111. }
  112. }
  113.  
  114. IMobileServiceTable<TodoItem> todoTable =
  115. App.MobileService.GetTable<TodoItem>();
  116. MobileServiceCollection<TodoItem, TodoItem> items;
  117.  
  118.  
  119.  
  120.  
  121. public class TodoItem
  122. {
  123. public string Id { get; set; }
  124. public string Title { get; set; }
  125. public bool Complete { get; set; }
  126. public string FirstName { get; set; }
  127. public string LastName { get; set; }
  128. public string ISBN { get; set; }
  129. public string PublicationDate { get; set; }
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136. async private void Submit_Click(object sender, RoutedEventArgs e)
  137. {
  138.  
  139. try
  140. {
  141. TodoItem item = new TodoItem
  142. {
  143. Title = txtBxTitle.Text,
  144. FirstName = txtBxFName.Text,
  145. LastName = txtBxLName.Text,
  146. ISBN = txtBxISBN.Text,
  147. PublicationDate = txtBxPubDate.Text,
  148. Complete = false
  149. };
  150. await App.MobileService.GetTable<TodoItem>().InsertAsync(item);
  151. var dialog = new MessageDialog("Successful!");
  152. await dialog.ShowAsync();
  153. }
  154. catch (Exception em)
  155. {
  156. var dialog = new MessageDialog("An Error Occured: " +
  157. em.Message);
  158. await dialog.ShowAsync();
  159. }
  160. }
  161.  
  162. private async Task RefreshTodoItems()
  163. {
  164. MobileServiceInvalidOperationException exception = null;
  165. try
  166. {
  167. // This code refreshes the entries in the list view by querying
  168. the TodoItems table.
  169. // The query excludes completed TodoItems
  170. items = await todoTable
  171. .Where(TodoItem => TodoItem.Complete == false)
  172. .ToCollectionAsync();
  173. }
  174. catch (MobileServiceInvalidOperationException e)
  175. {
  176. exception = e;
  177. }
  178.  
  179. if (exception != null)
  180. {
  181. await new MessageDialog(exception.Message, "Error loading
  182. items").ShowAsync();
  183. }
  184. else
  185. {
  186. ListItems.ItemsSource = items;
  187. this.btnRefresh.IsEnabled = true;
  188. }
  189. }
  190. private async void CheckBoxComplete_Checked(object sender, RoutedEventArgs
  191. e)
  192. {
  193. CheckBox cb = (CheckBox)sender;
  194. TodoItem item = cb.DataContext as TodoItem;
  195. await UpdateCheckedTodoItem(item);
  196. }
  197.  
  198. private async Task UpdateCheckedTodoItem(TodoItem item)
  199. {
  200. // This code takes a freshly completed TodoItem and updates the
  201. database. When the MobileService
  202. // responds, the item is removed from the list
  203.  
  204. await todoTable.UpdateAsync(item);
  205. items.Remove(item);
  206. ListItems.Focus(Windows.UI.Xaml.FocusState.Unfocused);
  207.  
  208. //await SyncAsync(); // offline sync
  209. }
Add Comment
Please, Sign In to add comment