Вывод картинки и уведомления

This commit is contained in:
2026-09-03 21:58:35 +03:00
parent a0e9fd89ff
commit 3ba8c7ffaa
6 changed files with 125 additions and 2 deletions
+5 -1
View File
@@ -1,10 +1,14 @@
<Application xmlns="https://github.com/avaloniaui" <Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonia="using:Material.Icons.Avalonia"
xmlns:semi="https://irihi.tech/semi"
x:Class="AvaloniaTest.App" x:Class="AvaloniaTest.App"
RequestedThemeVariant="Default"> RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles> <Application.Styles>
<FluentTheme /> <semi:SemiTheme/>
<semi:SemiPopupAnimations/>
<avalonia:MaterialIconStyles/>
</Application.Styles> </Application.Styles>
</Application> </Application>
+7
View File
@@ -15,5 +15,12 @@
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets> <IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets> <PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Material.Icons.Avalonia" Version="3.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
<PackageReference Include="Semi.Avalonia" Version="12.1.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+16
View File
@@ -0,0 +1,16 @@
<NavigationPage xmlns="https://github.com/avaloniaui"
xmlns:avalonia="using:Material.Icons.Avalonia"
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="AvaloniaTest.MainView">
<ContentPage Header="Welcome">
<StackPanel>
<Button Click="Button_Click">
<avalonia:MaterialIcon Kind="File"/>
</Button>
<Image Width="300" Height="200" Stretch="Uniform" Name="Image1"/>
</StackPanel>
</ContentPage>
</NavigationPage>
+89
View File
@@ -0,0 +1,89 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using System;
using System.IO;
using System.Threading.Tasks;
namespace AvaloniaTest
{
public partial class MainView : NavigationPage
{
public static MainView Current { get; private set; } = null!;
private WindowNotificationManager notificationManager = null!;
public MainView()
{
InitializeComponent();
Current = this;
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
notificationManager = new WindowNotificationManager(TopLevel.GetTopLevel(this));
}
/// <summary>
/// Показывает уведомление с заданным заголовком, сообщением и типом уведомления.
/// </summary>
/// <param name="title">Заголовок</param>
/// <param name="message">Содержимое</param>
/// <param name="type">Тип уведомления</param>
public void ShowNotification(string title, string message, NotificationType type)
{
notificationManager.Show(new Notification
{
Message = message,
Expiration = TimeSpan.FromSeconds(3),
Type = type,
Title = title
});
}
/// <summary>
/// Обработчик события нажатия кнопки для выбора изображения и его отображения в Image1.
/// </summary>
/// <param name="sender">Отправитель</param>
/// <param name="e">Аргументы</param>
private async void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
try
{
var top = TopLevel.GetTopLevel(this);
var result = await top!.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = false,
FileTypeFilter = [FilePickerFileTypes.ImageAll],
Title = "Выберите изображение"
});
if (result.Count == 0)
return;
var imagePath = result[0].Path.LocalPath;
string newImageName = $"{Guid.NewGuid()}{Path.GetExtension(imagePath)}";
string newImagePath = Path.Combine("Images", newImageName);
Directory.CreateDirectory("Images");
File.Copy(imagePath, newImagePath);
Image1.Source = new Bitmap(newImagePath);
MainView.Current.ShowNotification("Успех", "Изображение успешно загружено и сохранено.", NotificationType.Success);
}
catch (Exception ex)
{
MainView.Current.ShowNotification("Ошибка", $"Не удалось загрузить изображение: {ex.Message}", NotificationType.Error);
}
}
}
}
+3 -1
View File
@@ -1,9 +1,11 @@
<Window xmlns="https://github.com/avaloniaui" <Window xmlns="https://github.com/avaloniaui"
xmlns:avalonia="using:Material.Icons.Avalonia"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:AvaloniaTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaTest.MainWindow" x:Class="AvaloniaTest.MainWindow"
Title="AvaloniaTest"> Title="AvaloniaTest">
Welcome to Avalonia! <local:MainView/>
</Window> </Window>
+5
View File
@@ -8,4 +8,9 @@ public partial class MainWindow : Window
{ {
InitializeComponent(); InitializeComponent();
} }
private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
}
} }