Selaa lähdekoodia

Добвлено редактирование картинки профиля пользователя

unknown 1 viikko sitten
vanhempi
commit
e236ec2e27

BIN
AvaloniaHomeTry/Assets/Заглушка.jpg


+ 69 - 20
AvaloniaHomeTry/ViewModels/RegistrationPageViewModel.cs

@@ -1,35 +1,53 @@
 using System;
 using System.Collections.Generic;
 using System.Data;
+using System.IO;
 using System.Linq;
+using Avalonia.Controls.ApplicationLifetimes;
+using System.Threading.Tasks;
 using Avalonia.Media;
+using Avalonia.Media.Imaging;
+using Avalonia.Platform.Storage;
 using AvaloniaHomeTry.Models;
 using Microsoft.Win32;
 using ReactiveUI;
+using Avalonia;
+using Avalonia.Controls.Platform;
 
 namespace AvaloniaHomeTry.ViewModels
 {
-	public class RegistrationPageViewModel : ViewModelBase
-	{
-		UsersTable? currentUser;
-		
-		Gender? genderId;
-		List<Gender> genders;
+    public class RegistrationPageViewModel : ViewModelBase
+    {
 
-		string login;
-		string password;
-		string surname;
-		string name;
-		string patronymic;
-		DateTime dob = DateTime.UtcNow;
+        UsersTable? currentUser;
+
+        List<UsersTable> mainUsers;
+
+        List<UsersTable> secondUsers;
+
+        Gender? genderId;
+        List<Gender> genders;
+
+        string login;
+        string password;
+        string surname;
+        string name;
+        string patronymic;
+        DateTime dob = DateTime.UtcNow;
         private string message;
+        Bitmap? userImage;
 
 
         public RegistrationPageViewModel()
-		{
-			this.genders = db.Genders.ToList();
-		}
+        {
+            this.genders = db.Genders.ToList();
+        }
+        public RegistrationPageViewModel(int idUser)
+        {
+            UserImage = CurrentUser.Image != null ? new Bitmap(new MemoryStream(CurrentUser.Image)) : new Bitmap("D:\\avaloniax\\AvaloniaHomeTry\\Assets\\background.jpg");
 
+        }
+       
         public Gender? GenderId { get => genderId; set => this.RaiseAndSetIfChanged(ref genderId, value); }
         public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
         public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
@@ -39,6 +57,10 @@ namespace AvaloniaHomeTry.ViewModels
         public string Patronymic { get => patronymic; set => this.RaiseAndSetIfChanged(ref patronymic, value); }
         public DateTime Dob { get => dob; set => this.RaiseAndSetIfChanged(ref dob, value); }
         public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
+        public Bitmap? UserImage { get => userImage; set => this.RaiseAndSetIfChanged(ref userImage, value); }
+        public UsersTable? CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
+        public List<UsersTable> MainUsers { get => mainUsers; set => this.RaiseAndSetIfChanged(ref mainUsers, value); }
+        public List<UsersTable> SecondUsers { get => secondUsers; set => this.RaiseAndSetIfChanged(ref secondUsers, value); }
 
         public DateTimeOffset DateTimeOffset
         {
@@ -51,7 +73,7 @@ namespace AvaloniaHomeTry.ViewModels
 			UsersTable flag = db.UsersTable.FirstOrDefault(x => x.Login == Login);
             if (flag == null)
             {
-                currentUser = new UsersTable
+                CurrentUser = new UsersTable
                 {
                     Login = Login,
                     Password = Password,
@@ -60,13 +82,14 @@ namespace AvaloniaHomeTry.ViewModels
                     Patronymic = Patronymic,
                     Dataofbirth = Dob,
                     IdGender = genderId.Id,
-                    IdRole = 2
+                    IdRole = 2    
                 };
 
-                db.UsersTable.Add(currentUser);
+                Image(CurrentUser.Id);
+                db.UsersTable.Add(CurrentUser);
                 db.SaveChanges();
-                currentUser = db.UsersTable.FirstOrDefault(x => x.Login == Login);
-                MainWindowViewModel.Self.UC = new UserPage(currentUser.Id);
+                CurrentUser = db.UsersTable.FirstOrDefault(x => x.Login == Login);
+                MainWindowViewModel.Self.UC = new UserPage(CurrentUser.Id);
             }
             else 
             {
@@ -75,5 +98,31 @@ namespace AvaloniaHomeTry.ViewModels
 		}
 
         public void Back() => MainWindowViewModel.Self.UC = new AuthPage();
+        
+        public async Task Image(int id)
+        {
+            if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
+                desktop.MainWindow?.StorageProvider is not { } provider)
+                throw new NullReferenceException("Missing StorageProvider instance.");
+            {
+                var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
+                {
+                    Title = "Âûáèðèòå ôàéë ñ èçîáðàæåíèåì: ",
+                    AllowMultiple = false
+                });
+
+                await using var readStream = await files[0].OpenReadAsync();
+                byte[] buffer = new byte[10000000];
+                var bytes = readStream.ReadAtLeast(buffer, 1);
+                Array.Resize(ref buffer, bytes);
+                currentUser = db.UsersTable.FirstOrDefault(x => x.Id == id);
+                currentUser.Image = buffer;
+                UserImage = new Bitmap(new MemoryStream(currentUser.Image));
+                db.SaveChanges();
+                SecondUsers = db.UsersTable.ToList();
+                MainUsers = SecondUsers;
+
+            }
+        }
     }
 }

+ 1 - 1
AvaloniaHomeTry/ViewModels/UserPageViewModel.cs

@@ -32,7 +32,7 @@ namespace AvaloniaHomeTry.ViewModels
 		{
 
 			currentUser = db.UsersTable.Include(x => x.GenderNavigation).Include(x => x.RoleNavigation).FirstOrDefault(x => x.Id == idUser);
-            UserImage = currentUser.Image != null ? new Bitmap(new MemoryStream(currentUser.Image)) : new Bitmap("Assets/background.jpg");
+            UserImage = CurrentUser.Image != null ? new Bitmap(new MemoryStream(CurrentUser.Image)) : new Bitmap("D:\\avaloniax\\AvaloniaHomeTry\\Assets\\background.jpg");
         }
 
 		public List<string> Genders => db.Genders.Select(x => x.GenderName).ToList();

+ 21 - 10
AvaloniaHomeTry/Views/RegistrationPage.axaml

@@ -19,33 +19,38 @@
 			<RowDefinition Height="50"/>
 			<RowDefinition Height="50"/>
 		</Grid.RowDefinitions>
-		<StackPanel>
-			<TextBlock Text="Регистрация" Grid.Row="0" HorizontalAlignment="Center" Margin="20"/>
+		<Grid.ColumnDefinitions>
+			<ColumnDefinition Width="*"/>
+			<ColumnDefinition Width="*"/>
+			<ColumnDefinition Width="*"/>
+		</Grid.ColumnDefinitions>
+		<StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Margin="20">
+			<TextBlock Text="Регистрация" />
 		</StackPanel>
 		<StackPanel HorizontalAlignment="Center" Grid.Row="1" Margin="5">
 			<TextBlock Text="Логин:"/>
 			<TextBox Text="{Binding Login}" Width="200"/>
 			<TextBlock Text="{Binding Message}"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="2" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="10">
 			<TextBlock Text="Пароль:"/>
 			<TextBox Text="{Binding Password}" Width="200"/>
 			<TextBlock Text="{Binding Message}"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="3" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="2" Grid.Column="0" Margin="10">
 			<TextBlock Text="Фамилия:"/>
 			<TextBox Text="{Binding Surname}" Width="200"/>
 			<TextBlock Text="{Binding Message}"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="4" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="2" Grid.Column="1" Margin="10">
 			<TextBlock Text="Имя:"/>
 			<TextBox Text="{Binding Name}" Width="200"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="5" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="2" Grid.Column="2" Margin="10">
 			<TextBlock Text="Отчество:"/>
 			<TextBox Text="{Binding Patronymic}" Width="200"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="6" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="3" Grid.Column="0" Margin="10">
 			<TextBlock Text="Пол:"/>
 			<ComboBox ItemsSource="{Binding Genders}" SelectedItem="{Binding GenderId}" Width="200" >
 				<ComboBox.ItemTemplate>
@@ -55,14 +60,20 @@
 				</ComboBox.ItemTemplate>
 			</ComboBox>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="7" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="3" Grid.Column="1" Margin="10">
 			<TextBlock Text="Дата рождения:"/>
 			<DatePicker SelectedDate="{Binding DateTimeOffset}"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="8" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="3" Grid.Column="2">
+			<Image Source="{Binding UserImage}" Width="200" Height="200"/>			
+		</StackPanel>
+		<StackPanel Grid.Row="4" Grid.Column="2" HorizontalAlignment="Center">
+			<Button Content="Выбрать изображение" Command="{Binding Image}" CommandParameter="{Binding CurrentUser.Id}"/>
+		</StackPanel>
+		<StackPanel HorizontalAlignment="Center" Grid.Row="6" Grid.Column="1" Margin="10">
 			<Button Content="Зарегистрироваться" Command="{Binding Reg}"/>
 		</StackPanel>
-		<StackPanel HorizontalAlignment="Center" Grid.Row="9" Margin="10">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="7" Grid.Column="1" Margin="10">
 			<Button Content="Назад" Command="{Binding Back}"/>
 		</StackPanel>
 	</Grid>

+ 8 - 8
AvaloniaHomeTry/Views/UserPage.axaml

@@ -30,29 +30,29 @@
 		</StackPanel>		
 		
 		<StackPanel HorizontalAlignment="Center" Grid.Row="1" Grid.Column="2" Margin="10">
-			<Button Content="Изменить изображение" Command="{Binding Image}" CommandParameter="{Binding CurrentUser.Id}" IsEnabled="True"/>
+			<Button Content="Изменить изображение" Command="{Binding Image}" CommandParameter="{Binding CurrentUser.Id}"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="2" Grid.Column="0" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Grid.Column="0" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Фамилия"/>
 			<TextBox Text="{Binding CurrentUser.Surname}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="2" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Имя"/>
 			<TextBox Text="{Binding CurrentUser.Firstname}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="2" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Отчество"/>
 			<TextBox Text="{Binding CurrentUser.Patronymic}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
-		<StackPanel Grid.Row="3" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="4" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Логин"/>
 			<TextBox Text="{Binding CurrentUser.Login}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="3" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="4" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Пол"/>
 			<ComboBox ItemsSource="{Binding Genders}" 
 					  SelectedItem="{Binding GenderIndex}" 
@@ -60,12 +60,12 @@
 
 		</StackPanel>
 			
-		<StackPanel Grid.Row="3" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="4" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Дата рождения"/>
 			<DatePicker SelectedDate="{Binding DR}"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="4" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="5" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Роль в системе:"/>
 			<TextBlock Text="{Binding CurrentUser.RoleNavigation.Role}"/>
 		</StackPanel>