Вывод картинки и уведомления
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
<Application xmlns="https://github.com/avaloniaui"
|
||||
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"
|
||||
RequestedThemeVariant="Default">
|
||||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
|
||||
|
||||
<Application.Styles>
|
||||
<FluentTheme />
|
||||
<semi:SemiTheme/>
|
||||
<semi:SemiPopupAnimations/>
|
||||
<avalonia:MaterialIconStyles/>
|
||||
</Application.Styles>
|
||||
</Application>
|
||||
@@ -15,5 +15,12 @@
|
||||
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
||||
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
||||
</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>
|
||||
</Project>
|
||||
|
||||
@@ -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>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
<Window 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:local="using:AvaloniaTest"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="AvaloniaTest.MainWindow"
|
||||
Title="AvaloniaTest">
|
||||
Welcome to Avalonia!
|
||||
<local:MainView/>
|
||||
</Window>
|
||||
|
||||
@@ -8,4 +8,9 @@ public partial class MainWindow : Window
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user