1 xamarin.ios xamarin.android xamarin xamarin.forms skiasharp
如何在 Xamarin Forms(Android 和 iOS)上.svg使用SkiaSharp加载文件。
我尝试使用 SkiaSharp 在 Xamarin 中加载 SVG 文件,但无法加载它。
我尝试使用SkiaSharp.Extended并参考他们的演示,但仍然无法加载它。
我做错了什么吗?
请帮我!
B1Images >在您的项目中创建一个文件夹Xamarin forms并.svg在其中保存图像。确保选择Build Action“EmbeddedResource”
B2 > 下载所需的库,例如:
SkiaSharp, SkiaSharp.Views.Forms,SkiaSharp.Extended.Svg
B3 > At behind code,使用LoadSvg(xCanvasView.AutomationId)Page 的构造函数 ( Example: Page is ListNewConversationPage) 处的方法。并声明所需的函数:
private SKSvg svg;
// Get file .svg to folder Images
private static Stream GetImageStream(string svgName)
{
var type = typeof(ListNewConversationPage).GetTypeInfo();
var assembly = type.Assembly;
var abc = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Images.{svgName}");
return abc;
}
private void LoadSvg(string svgName)
{
// create a new SVG object
svg = new SKSvg();
// load the SVG document from a stream
using (var stream = GetImageStream(svgName))
svg.Load(stream);
}
private void OnPageAppearing(object sender, EventArgs e)
{
svg = null;
var page = (ContentPage)sender;
LoadSvg(page.AutomationId);
var canvas = (SKCanvasView)page.Content;
canvas.InvalidateSurface();
}
private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
{
try
{
var surface = e.Surface;
var canvas = surface.Canvas;
var width = e.Info.Width;
var height = e.Info.Height;
// clear the surface
canvas.Clear(SKColors.White);
// the page is not visible yet
if (svg == null)
return;
// calculate the scaling need to fit to screen
float scaleX = width / svg.Picture.CullRect.Width;
float scaleY = height / svg.Picture.CullRect.Height;
var matrix = SKMatrix.MakeScale(scaleX, scaleY);
// draw the svg
canvas.DrawPicture(svg.Picture, ref matrix);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
B4 > 在.xaml文件中,将其与.svg图像一起使用image_part_circle.svg
<forms:SKCanvasView x:Name="xCanvasView"
PaintSurface="OnPainting"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
BackgroundColor="Blue"
AutomationId="image_part_circle.svg" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7071 次 |
| 最近记录: |