1.项目下载地址:https://gitee.com/confusedkitten/avalonia-demo
3.SVG库,Avalonia.Svg.Skia,项目地址 https://github.com/wieslawsoltes/Svg.Skia
4.样式预览:
5.Svg.axaml
<UserControl 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="AvaloniaDemo.Pages.Svg"><StackPanel Orientation="Horizontal" VerticalAlignment="Top"><StackPanel Height="100" Width="100" Margin="20 0 20 0" ><TextBlock Text="这是一个png"/><Image Source="../Assets/Images/JiaHao.png" Height="25" Width="25"/></StackPanel><StackPanel Height="100" Width="100" Margin="20 0 20 0" ><TextBlock Text="这是一个svg"/><Svg Path="/Assets/Images/__tiger.svg" Stretch="Uniform" /></StackPanel><StackPanel Height="100" Width="120" Margin="20 0 20 0" IsVisible="True"><TextBlock Text="后台代码设置的svg"/><Image x:Name="ShowImage" VerticalAlignment="Center" HorizontalAlignment="Center" Height="100" Width="100" Stretch="Uniform"/></StackPanel></StackPanel>
</UserControl>
6.Svg.axaml.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Svg.Skia;
using System.IO;
using System;
using Avalonia.Interactivity;
using Avalonia.Controls.Shapes;namespace AvaloniaDemo.Pages;public partial class Svg : UserControl
{public Svg(){InitializeComponent();Loaded += WindowLoaded;}private void WindowLoaded(object? sender, RoutedEventArgs e){string baseurl = Directory.GetCurrentDirectory();var imagepath = System.IO.Path.Combine(baseurl, @"Assets\Images\new 4.svg");var ImageByte = File.ReadAllBytes(imagepath);var svgSource = ByteArrayToDrawingImage(ImageByte);ShowImage.Source = svgSource;}public SvgImage? ByteArrayToDrawingImage(Byte[] byteArray){if (byteArray == null || byteArray?.Length == 0) return default;using (MemoryStream documentStream = new MemoryStream(byteArray)){try{var svg = new SvgSource();var picture = svg.Load(documentStream);if (picture is { }){var svgImage = new Avalonia.Svg.Skia.SvgImage() { Source = svg };return svgImage;}}catch (Exception ex){}}return default;}
}