This commit is contained in:
KP9lKk 2024-12-26 10:39:55 +03:00
parent 3d9093c648
commit b8bbd43b28
15 changed files with 344 additions and 0 deletions

16
AppForKids.sln Normal file
View File

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppForKids", "AppForKids\AppForKids.csproj", "{4E7B7257-224A-465E-84C9-AF7DC5D9FF3E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4E7B7257-224A-465E-84C9-AF7DC5D9FF3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E7B7257-224A-465E-84C9-AF7DC5D9FF3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E7B7257-224A-465E-84C9-AF7DC5D9FF3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E7B7257-224A-465E-84C9-AF7DC5D9FF3E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

11
AppForKids/App.axaml Normal file
View File

@ -0,0 +1,11 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AppForKids.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>

23
AppForKids/App.axaml.cs Normal file
View File

@ -0,0 +1,23 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
namespace AppForKids;
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new Registration();
}
base.OnFrameworkInitializationCompleted();
}
}

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.2.1"/>
<AvaloniaResource Include="assets/**" />
<PackageReference Include="Avalonia.Desktop" Version="11.2.1"/>
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.1"/>
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.1"/>
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="Avalonia.Diagnostics" Version="11.2.1">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,19 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AppForKids.MainWindow"
Title="AppForKids">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10">
<TextBlock/>
<Button x:Name="ImageButton" Click="ImageButton_OnClick">
<Image Width="200"
Height="200"
Source="avares://AppForKids/assets/dotnet-bot_branded.png"
/>
</Button>
<TextBlock x:Name="ClickCounts"/>
<Button Width="200" Content="Открыть счет" x:Name="OpenScoreButton" Click="OpenScoreButton_OnClick"/>
</StackPanel>
</Window>

View File

@ -0,0 +1,36 @@
using AppForKids.models;
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace AppForKids;
public partial class MainWindow : Window
{
private User? _currentUser = null;
public MainWindow(User user)
{
_currentUser = user;
InitializeComponent();
CountClickText();
}
private void CountClickText()
{
if (_currentUser == null) return;
ClickCounts.Text = $"Click count: {_currentUser.Clicks}";
}
private void OpenScoreButton_OnClick(object? sender, RoutedEventArgs e)
{
if(_currentUser == null) return;
new Score().ShowDialog(this);
}
private void ImageButton_OnClick(object? sender, RoutedEventArgs e)
{
using var dbContext = new KidsAppDbContext();
if(_currentUser == null) return;
_currentUser.Clicks += 1;
dbContext.Update(_currentUser);
if(dbContext.SaveChanges() > 0) CountClickText();
}
}

21
AppForKids/Program.cs Normal file
View File

@ -0,0 +1,21 @@
using Avalonia;
using System;
namespace AppForKids;
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}

View File

@ -0,0 +1,18 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AppForKids.Registration"
Title="Registration">
<StackPanel Spacing="15" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Введите логин"/>
<TextBox Width="300" x:Name="LoginTextBox" ></TextBox>
<TextBlock Text="Введите пароль"/>
<TextBox Width="300" PasswordChar="*" x:Name="PasswordTextBox" ></TextBox>
<StackPanel Spacing="5" Orientation="Horizontal">
<Button x:Name="RegisterClick" Click="RegisterClick_OnClick" Content="Зарегистрироваться"/>
<Button x:Name="LoginClick" Click="LoginClick_OnClick" Content="Войти"/>
</StackPanel>
</StackPanel>
</Window>

View File

@ -0,0 +1,47 @@
using System.Globalization;
using System.IO;
using System.Linq;
using AppForKids.models;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace AppForKids;
public partial class Registration : Window
{
public Registration()
{
InitializeComponent();
}
private void RegisterClick_OnClick(object? sender, RoutedEventArgs e)
{
if(string.IsNullOrEmpty(LoginTextBox.Text)) return;
if(string.IsNullOrEmpty(PasswordTextBox.Text)) return;
using var dbContext = new KidsAppDbContext();
var user = new User {
Clicks = 0,
Name = LoginTextBox.Text,
Password = PasswordTextBox.Text };
dbContext.Users.Add(user);
if (dbContext.SaveChanges() > 0)
{
new MainWindow(user).Show();
Close();
}
}
private void LoginClick_OnClick(object? sender, RoutedEventArgs e)
{
using var dbContext = new KidsAppDbContext();
if(string.IsNullOrEmpty(LoginTextBox.Text)) return;
if(string.IsNullOrEmpty(PasswordTextBox.Text)) return;
var user = dbContext.Users.FirstOrDefault(it => it.Name == LoginTextBox.Text);
if(user == null) return;
if (user.Password != PasswordTextBox.Text) return;
new MainWindow(user).Show();
Close();
}
}

25
AppForKids/Score.axaml Normal file
View File

@ -0,0 +1,25 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:CompileBindings="False"
x:Class="AppForKids.Score"
Title="Score">
<DockPanel>
<ListBox x:Name="ScoreListBox" Scroll="">
<ListBox.ItemTemplate>
<DataTemplate>
<Border
BorderThickness="3"
BorderBrush="Black">
<StackPanel Spacing="5" Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Clicks}"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Window>

21
AppForKids/Score.axaml.cs Normal file
View File

@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using AppForKids.models;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Microsoft.EntityFrameworkCore.Metadata;
using Color = System.Drawing.Color;
namespace AppForKids;
public partial class Score : Window
{
public Score()
{
InitializeComponent();
var dbContext = new KidsAppDbContext();
ScoreListBox.ItemsSource = dbContext.Users;
}
}

18
AppForKids/app.manifest Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embedded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="AppForKids.Desktop"/>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace AppForKids.models;
public partial class KidsAppDbContext : DbContext
{
public KidsAppDbContext()
{
}
public KidsAppDbContext(DbContextOptions<KidsAppDbContext> options)
: base(options)
{
}
public virtual DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseNpgsql("Host=localhost;Password=123;Database=kids_app_db;Port=5432;Username=postgres");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.Id).HasName("users_pkey");
entity.ToTable("users");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Clicks).HasColumnName("clicks");
entity.Property(e => e.Name).HasColumnName("name");
entity.Property(e => e.Password).HasColumnName("password");
entity.Property(e => e.Url).HasColumnName("url");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

17
AppForKids/models/User.cs Normal file
View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace AppForKids.models;
public partial class User
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int Clicks { get; set; }
public string Password { get; set; } = null!;
public string? Url { get; set; }
}