avalonia-controls-media
Use when working with Avalonia media controls: Image, DrawingImage (incl. Viewbox 12.0+), PathIcon, MediaPlayerControl (LibVLCSharp), or NativeWebView/NativeWebDialog (Avalonia.Controls.WebView, open source in 12.0+, commercial in 11.x). Covers asset loading URIs, stretch modes, vector icons, video playback setup, and web content embedding.
linuxdevel · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add linuxdevel/Avalonia-skills --skill avalonia-controls-media技能说明
name: avalonia-controls-media description: Use when working with Avalonia media controls: Image, DrawingImage (incl. Viewbox 12.0+), PathIcon, MediaPlayerControl (LibVLCSharp), or NativeWebView/NativeWebDialog (Avalonia.Controls.WebView, open source in 12.0+, commercial in 11.x). Covers asset loading URIs, stretch modes, vector icons, video playback setup, and web content embedding.
Avalonia Media Controls
Overview
Media controls display images, vector graphics, and embedded web or video content. Image sources use avares:// URIs for app assets. Vector content (PathIcon, DrawingImage) scales without pixelation.
Image
Displays raster (PNG, JPEG, BMP, WebP) or vector images.
| Property | Type | Notes |
|---|---|---|
Source | IImage | Bitmap, DrawingImage, bound IImage |
Stretch | Stretch | None, Fill, Uniform (default), UniformToFill |
Width / Height | double | Explicit size; required if parent gives infinite space |
RenderOptions.BitmapInterpolationMode | BitmapInterpolationMode | Default, LowQuality, MediumQuality, HighQuality, None |
Stretch modes:
| Value | Behavior |
|---|---|
None | Original pixel size |
Fill | Stretches to fill, distorts aspect ratio |
Uniform | Letterboxed to fit, preserves aspect ratio |
UniformToFill | Fills area, may clip, preserves aspect ratio |
Loading from app assets (AXAML):
<!-- Bundled asset — requires Build Action: AvaloniaResource -->
<Image Source="avares://MyApp/Assets/logo.png" Width="120" Height="40" Stretch="Uniform"/>
<!-- Relative path from project root (works at design time, runtime needs avares://) -->
<Image Source="/Assets/banner.jpg" Stretch="UniformToFill"/>
Loading in code:
using Avalonia.Platform;
using Avalonia.Media.Imaging;
// From app assets
var uri = new Uri("avares://MyApp/Assets/logo.png");
var bitmap = new Bitmap(AssetLoader.Open(uri));
myImage.Source = bitmap;
// From file system
var bitmap = new Bitmap("/home/user/photo.png");
myImage.Source = bitmap;
// From stream
using var stream = File.OpenRead(path);
var bitmap = new Bitmap(stream);
Binding to ViewModel:
// In ViewModel
private Bitmap? _photo;
public Bitmap? Photo
{
get => _photo;
set => SetProperty(ref _photo, value);
}
// Load async
public async Task LoadPhotoAsync(string path)
{
await using var stream = File.OpenRead(path);
Photo = await Task.Run(() => new Bitmap(stream));
}
<Image Source="{Binding Photo}" Width="200" Height="200" Stretch="UniformToFill"/>
Dispose Bitmaps when no longer needed to release native memory:
_photo?.Dispose();
DrawingImage
Vector image source built from geometry. Use for scalable icons embedded as image sources.
| Class | Use |
|---|---|
DrawingGroup | Combines multiple drawings |
GeometryDrawing | Fills/strokes a geometry path |
ImageDrawing | Embeds a bitmap in a drawing |
<Image Width="64" Height="64">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup>
<!-- Background circle -->
<GeometryDrawing Brush="#6200EE"
Geometry="M 32,32 m -32,0 a 32,32 0 1,0 64,0 a 32,32 0 1,0 -64,0"/>
<!-- Foreground path -->
<GeometryDrawing Brush="White"
Geometry="M 20,20 L 44,32 L 20,44 Z"/>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
DrawingImage.Viewbox (12.0+) — Rect? in DIPs: which region of the Drawing to render. Crops the drawing or adds empty space around it. null (default) = the drawing's own bounds. Not in 11.x.
<DrawingImage Viewbox="0,0,64,64" Drawing="{StaticResource IconDrawing}"/>
Prefer
PathIconfor simple icons. UseDrawingImagewhen you need composite layered vector drawings or need to embed asImage.Source.
PathIcon
SVG-path-based scalable icon. Preferred for UI icons. Color controlled by Foreground.
| Property | Type | Notes |
|---|---|---|
Data | Geometry | SVG path string |
Foreground | IBrush | Icon color (inherits from parent) |
Width / Height | double | Display size |
<!-- Basic icon -->
<PathIcon Data="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"
Width="24" Height="24" Foreground="{DynamicResource TextFillColorPrimaryBrush}"/>
<!-- Icon button -->
<Button>
<PathIcon Data="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12z" Width="20" Height="20"/>
</Button>
<!-- Colored icon -->
<PathIcon Data="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 14.5v-9l6 4.5-6 4.5z"
Width="32" Height="32" Foreground="#4CAF50"/>
<!-- Inside a style/template, inheriting parent foreground -->
<PathIcon Data="..." Width="16" Height="16"/>
Dynamic path from ViewModel:
<PathIcon Data="{Binding IconPathData}" Width="24" Height="24"/>
// In ViewModel
public string IconPathData => IsPlaying
? "M6 19h4V5H6v14zm8-14v14h4V5h-4z" // pause icon
: "M8 5v14l11-7z"; // play icon
MediaPlayerControl (LibVLCSharp)
Avalonia does not include built-in video playback. Use LibVLCSharp.Avalonia.
NuGet packages:
LibVLCSharp
LibVLCSharp.Avalonia
VideoLAN.LibVLC.Windows (or .Mac / .Linux)
Namespace: xmlns:vlc="using:LibVLCSharp.Avalonia"
<vlc:VideoView x:Name="VideoView" MediaPlayer="{Binding MediaPlayer}">
<!-- Overlay controls rendered on top of video -->
<Grid VerticalAlignment="Bottom" Background="#80000000">
<StackPanel Orientation="Horizontal" Margin="8">
<Button Content="⏮" Command="{Binding StopCommand}"/>
<Button Content="{Binding PlayPauseLabel}" Command="{Binding PlayPauseComma
阅读完整来源 ↗