AuthViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using AuthAndCaptcha.Models;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Shapes;
  7. using Avalonia.Media;
  8. using ReactiveUI;
  9. namespace AuthAndCaptcha.ViewModels
  10. {
  11. public class AuthViewModel : ReactiveObject
  12. {
  13. string usersLogin;
  14. string usersPassword;
  15. string canText;
  16. string canTextTrue;
  17. bool authButtonVisible = true;
  18. bool canTextVisible = false;
  19. public string UsersLogin { get => usersLogin; set => this.RaiseAndSetIfChanged(ref usersLogin, value); }
  20. public string UsersPassword { get => usersPassword; set => this.RaiseAndSetIfChanged(ref usersPassword, value); }
  21. public string CanText { get => canText; set => this.RaiseAndSetIfChanged(ref canText, value); }
  22. public string CanTextTrue { get => canTextTrue; set => this.RaiseAndSetIfChanged(ref canTextTrue, value); }
  23. public bool AuthButtonVisible { get => authButtonVisible; set => this.RaiseAndSetIfChanged(ref authButtonVisible, value); }
  24. public bool CanTextVisible { get => canTextVisible; set => this.RaiseAndSetIfChanged(ref canTextVisible, value); }
  25. Canvas can;
  26. public Canvas Can { get => can; set => this.RaiseAndSetIfChanged(ref can, value); }
  27. public void CreateCaptcha()
  28. {
  29. Random rnd = new Random();
  30. SolidColorBrush color = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(255)), Convert.ToByte(rnd.Next(255))));
  31. FontStyle[] fontStyles = new FontStyle[2] { FontStyle.Normal, FontStyle.Italic };
  32. FontWeight[] fontWeights = new FontWeight[2] { FontWeight.Normal, FontWeight.Bold };
  33. int dist = 10;
  34. Canvas canvas = new Canvas()
  35. {
  36. Width = 400,
  37. Height = 400,
  38. Background = color
  39. };
  40. int len = rnd.Next(7, 10);
  41. for (int i = 0; i < 20; i++)
  42. {
  43. SolidColorBrush colorLine = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(255)), Convert.ToByte(rnd.Next(255))));
  44. Line line = new Line()
  45. {
  46. StartPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
  47. EndPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
  48. Stroke = colorLine,
  49. StrokeThickness = 3,
  50. };
  51. canvas.Children.Add(line);
  52. }
  53. for (int i = 0; i < len; i++)
  54. {
  55. FontStyle randomFontStyle = (FontStyle)rnd.Next(0, 3);
  56. string sym;
  57. int k = rnd.Next(0,2);
  58. if (k==0)
  59. {
  60. sym = Convert.ToString((char)rnd.Next(48, 57));
  61. }
  62. else
  63. {
  64. sym = Convert.ToString((char)rnd.Next(65, 90));
  65. }
  66. TextBlock text = new TextBlock()
  67. {
  68. Text = sym,
  69. FontSize = 20,
  70. Foreground = Brushes.Black,
  71. Margin = new Avalonia.Thickness(dist, rnd.Next(300), rnd.Next(300), 0),
  72. FontStyle = fontStyles[rnd.Next(0, 2)],
  73. FontWeight = fontWeights[rnd.Next(0, 2)]
  74. };
  75. canTextTrue = canTextTrue + Convert.ToString(text.Text);
  76. canvas.Children.Add(text);
  77. dist = dist + 35;
  78. }
  79. Debug.WriteLine(canTextTrue);
  80. Can = canvas;
  81. }
  82. }
  83. }