背景说明
在Avalonia Ul框架中,绑定图片资源失败,不显示,在给Source赋值时应该使用Avalonia.Avalonia.Media.lmaging.Bitmap类型,通过该方法new
Bitmap(AssetLoader.Open(new Uril"avares:/Demo RPG/lmages/Locations/Home.png")来解析属性已设置为AvaloniaResource的图片资源
Avalonia UI框架下的绑定
- .axaml中
<Image Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="125"
Width="125"
Source="{Binding CurrentLocation.ImageName}"/>
- 对应ViewModel中
CurrentLocation = new Location
{
Name = "Home",
XCoordinate = 0,
YCoordinate = -1,
Description = "This is your house",
ImageName = new Bitmap(AssetLoader.Open(new Uri("avares://Demo_RPG/Images/Locations/Home.png")))
};
- 参考链接
https://docs.avaloniaui.net/zh-Hans/docs/basics/user-interface/assets