Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. public abstract class WPFViewModelCommon : WPFViewModelBase
  2. {
  3.  
  4. // public abstract UserVM User{get; set;} // так + override в дочернем классе - работает
  5. private UserVM _user;
  6. public override UserVM User
  7. {
  8. get { return _user; }
  9. set { _user = value; OnPropertyChanged("User"); }
  10. }
  11.  
  12. private string _textblockcontent;
  13. public string TextBlockContent
  14. {
  15. get { return _textblockcontent; }
  16. set { _textblockcontent = value; OnPropertyChanged("TextBlockContent"); }
  17. }
  18.  
  19. private string _buttoncontent;
  20. public string ButtonContent
  21. {
  22. get { return _buttoncontent; }
  23. set { _buttoncontent = value; OnPropertyChanged("ButtonContent"); }
  24. }
  25.  
  26. private ImageSource _avataronform;
  27. public ImageSource AvatarOnForm
  28. {
  29. get { return _avataronform; }
  30. set
  31. {
  32. _avataronform = value; OnPropertyChanged("AvatarOnForm");
  33. }
  34. }
  35.  
  36. public abstract ICommand FinalAction { get; set; }
  37.  
  38. protected void LoadAvatarMethod()
  39. {
  40. IFileService fileService = new ImageFileService();
  41. IDialogService dialogService = new WPFClient.FileService.DialogService();
  42.  
  43. try
  44. {
  45. if (dialogService.OpenFileDialog() == true)
  46. {
  47. User.Avatar = fileService.Open(dialogService.FilePath); // падает здесь
  48. (fileService as ImageFileService).Dispose();
  49. dialogService.ShowMessage("Файл загружен");
  50. using (var stream = new FileStream(dialogService.FilePath, FileMode.Open))
  51. {
  52. AvatarOnForm = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
  53. OnPropertyChanged("AvatarOnForm");
  54. }
  55. }
  56. (dialogService as DialogService).Dispose();
  57. }
  58.  
  59. catch (Exception ex)
  60. {
  61. dialogService.ShowMessage(ex.Message);
  62. }
  63.  
  64. }
  65. }
  66.  
  67.  
  68.  
  69.  
  70. public class WPFViewModelRegistration : WPFViewModelCommon
  71.  
  72. {
  73.  
  74.  
  75. public string LoadButtonContent { get; set; }
  76.  
  77. public ICommand LoadAvatarCommand { get; set; }
  78. public override ICommand FinalAction { get; set; }
  79.  
  80.  
  81. public WPFViewModelRegistration()
  82. {
  83. FinalAction = new RelayCommand(arg => RegistrationMethod());
  84. LoadAvatarCommand = new RelayCommand(arg => LoadAvatarMethod());
  85. TextBlockContent = "Окно регистрации";
  86. ButtonContent = "Зарегистрироваться";
  87. LoadButtonContent = "Загрузить аватар";
  88. User = new UserVM();
  89. if (client==null)
  90. {
  91. client = new HttpClient();
  92. HttpClientSettings();
  93. }
  94. }
  95.  
  96. private async void RegistrationMethod()
  97. {
  98.  
  99. //post query
  100.  
  101. if (User.Login == null||User.Login == ""||User.Password == null || User.Password == "")
  102. {
  103. System.Windows.Forms.MessageBox.Show("Неверные данные. Не введен логин или пароль!");
  104. return;
  105. }
  106.  
  107. HttpResponseMessage testresponse = await client.GetAsync(new StringBuilder()
  108. .Append("api/userapi")
  109. .Append("?")
  110. .Append("login=")
  111. .Append(User.Login)
  112. .Append("&")
  113. .Append("password=")
  114. .Append(User.Password).
  115. ToString());
  116.  
  117. if (testresponse.StatusCode == HttpStatusCode.NotFound)
  118. {
  119. HttpResponseMessage response = await client.PostAsync("api/userapi", new StringContent(JsonConvert.SerializeObject(User)));
  120. if(response.StatusCode == HttpStatusCode.OK)
  121. {
  122. System.Windows.Forms.MessageBox.Show("Поздравляю! Вы успешно зарегистрировлаись в системе");
  123. }
  124. else
  125. {
  126. System.Windows.Forms.MessageBox.Show("Ошибка сервера. Повсторите ввод данных");
  127. }
  128. }
  129. else if (testresponse.StatusCode == HttpStatusCode.OK)
  130. {
  131. System.Windows.Forms.MessageBox.Show("такой пользователь уже есть в системе. Введите другие данные");
  132. return;
  133. }
  134. else
  135. {
  136. System.Windows.Forms.MessageBox.Show("Ошибка сервера. Повсторите ввод данных");
  137. return;
  138. }
  139.  
  140.  
  141. }
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement