MainWindow.axaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Avalonia.Controls;
  2. using Avalonia.Interactivity;
  3. using Avalonia.Markup.Xaml.MarkupExtensions;
  4. using Avalonia.Media;
  5. using System;
  6. using System.IO;
  7. namespace AvaloniaApplication2
  8. {
  9. // ñòðóêòóðà
  10. public struct Countries
  11. {
  12. public string name; // íàçâàíèå ñòðàíû
  13. public string capital; // ñòîëèöà
  14. public string form; // ôîðìà ïðàâëåíèÿ
  15. public string continent; // ìàòåðèê
  16. public string religions; // ðåëèãèè
  17. public bool isNATO; // ÿâëÿåòñÿ ëè ÷ëåíîì ÍÀÒÎ
  18. }
  19. public partial class MainWindow : Window
  20. {
  21. public MainWindow()
  22. {
  23. InitializeComponent();
  24. // cbContinent.SelectedIndex = 0;
  25. }
  26. // îáðàáîò÷èê ñîáûòèÿ Click äëÿ çàïèñè â ôàéë
  27. private void WriteFile(object sender, RoutedEventArgs e)
  28. {
  29. Countries country = new Countries(); // ñîçäàíèå îáúåêòà ñòðóêòóðû
  30. // çàïîëíåíèå ïîëåé ñòðóêòóðû
  31. country.name = tbName.Text; // çàïîëíåíèå íàçâàíèÿ ñòðàíû èç òåêñòîâîãî ïîëÿ
  32. country.capital = tbCapital.Text; // çàïîëíåíèå ñòîëèöû èç òåêñòîâîãî ïîëÿ
  33. // â çàâèñèìîñòè îò òîãî, êàêàÿ ðàäèîêíîïêà àêòèâíà, ïðîèñõîäèò çàïèñü ôîðìû ïðàâëåíèÿ
  34. if (rbM.IsChecked == true) country.form = "Ìîíàðõèÿ";
  35. if (rbK.IsChecked == true) country.form = "Êîíñòèòóöèÿ";
  36. if (rbD.IsChecked == true) country.form = "Äåìîêðàòèÿ";
  37. // åñëè ôëàæîê âûáðàí, òî çàïèñûâàåì ïðèíàäëåæíîñòü ñòðàíû ê ÍÀÒÎ
  38. if (cbNATO.IsChecked == true) country.isNATO = true;
  39. else country.isNATO = false;
  40. // çàïèñûâàåì êîíòèíåíò â çàâèñèìîñòè îò âûáðàííîãî çíà÷åíèÿ â ñïèñêå, îðèåíòèðóÿñü íà èíäåêñ âûáðàííîãî çíà÷åíèÿ (çíà÷åíèÿ íà÷èíàþòñÿ ñ èíäåêñà 0)
  41. switch (cbContinent.SelectedIndex)
  42. {
  43. case 1:
  44. country.continent = "Åâðàçèÿ";
  45. break;
  46. case 2:
  47. country.continent = "Àìåðèêà";
  48. break;
  49. case 3:
  50. country.continent = "Àôðèêà";
  51. break;
  52. }
  53. string religions = ""; // ñòðîêà äëÿ ôîðìèðîâàíèÿ ñâåäåíèé î ðåãèëèÿõ â ñòðàíå
  54. // ïðîáåãàåìñÿ ïî âñåìó ñïèñêó âûáðàííûõ ðåëèãèé (òàê êàê ó ñïèñêà íàñòðîåí ìíîæåñòâåííûé âûáîð ïðè óñëîâèè çàæàòèÿ Ctrl)
  55. foreach (TextBlock item in lbReligions.SelectedItems)
  56. {
  57. religions += item.Text + ", "; // äîáàâëÿåì ðåëèãèþ â ñòðîêó ÿåðåç çàïÿòóþ
  58. }
  59. country.religions = religions;
  60. // çàïèñü à ôàéë
  61. using (StreamWriter sw = new StreamWriter("users.csv",true,System.Text.Encoding.UTF8))
  62. {
  63. sw.WriteLine(country.name + ";" + country.capital + ";" + country.continent + ";" + country.form + ";" + country.religions + ";" + country.isNATO);
  64. }
  65. }
  66. public SolidColorBrush color(string continent)
  67. {
  68. switch (continent)
  69. {
  70. case "Åâðàçèÿ":
  71. return new SolidColorBrush(Color.FromArgb(50,255, 0, 0));
  72. case "Àìåðèêà":
  73. return new SolidColorBrush(Color.FromArgb(50, 0, 255, 0));
  74. case "Àôðèêà":
  75. return new SolidColorBrush(Color.FromArgb(50, 0, 0, 255));
  76. default:
  77. return new SolidColorBrush(Color.FromRgb(0, 0, 0));
  78. }
  79. }
  80. private void ShowCountriesClick(object sender, RoutedEventArgs e)
  81. {
  82. ShowCountries.IsVisible = true;
  83. WritePanel.IsVisible = false;
  84. using (StreamReader sr = new StreamReader("users.csv"))
  85. {
  86. string line ="";
  87. while (!sr.EndOfStream)
  88. {
  89. line = sr.ReadLine();
  90. string[] lineArray = line.Split(';');
  91. TextBlock tbName = new TextBlock()
  92. {
  93. Text = "Íàçâàíèå ñòðàíû: " + lineArray[0],
  94. };
  95. TextBlock tbCapital = new TextBlock()
  96. {
  97. Text = "Ñòîëèöà: "+lineArray[1]
  98. };
  99. TextBlock tbIsNATO = new TextBlock();
  100. if (Convert.ToBoolean(lineArray[5])) tbIsNATO.Text = "Âõîäèò ñ ÍÀÒÎ";
  101. else tbIsNATO.Text = "Íå âõîäèò â ñîñòàâ ÍÀÒÎ";
  102. StackPanel spCountry = new StackPanel();
  103. spCountry.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center;
  104. spCountry.Children.Add(tbName);
  105. spCountry.Children.Add(tbCapital);
  106. spCountry.Children.Add(tbIsNATO);
  107. Border border = new Border();
  108. border.Background = color(lineArray[2]);
  109. border.BorderBrush = Brushes.Indigo;
  110. border.BorderThickness = new Avalonia.Thickness(2);
  111. border.CornerRadius = new Avalonia.CornerRadius(50);
  112. border.Margin = new Avalonia.Thickness(10);
  113. border.Padding = new Avalonia.Thickness(20);
  114. border.Child = spCountry;
  115. ShowCountries.Children.Add(border);
  116. }
  117. }
  118. }
  119. private void Back_Click(object sender, RoutedEventArgs e)
  120. {
  121. ShowCountries.IsVisible = false;
  122. WritePanel.IsVisible = true;
  123. }
  124. }
  125. }