wpf - Avalonia Ui 相当于 ImageResource
问题描述
我尝试用 AvaloniaUi 定义一个 ImageSource。在 WPF 中,我是这样的:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="Icon">path/to/image/image.png</ImageSource>
</ResourceDictionary>
然后像这样引用它:
<Image Source="{StaticResource Icon}"/>
我如何在 Avalonia 中存档相同的内容?
解决方案
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:imaging="clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals">
<UserControl.Resources>
<imaging:Bitmap x:Key="MyBitmap">
<x:Arguments><x:String>icon.png</x:String></x:Arguments>
</imaging:Bitmap>
</UserControl.Resources>
<Image Source="{StaticResource MyBitmap}"
Width="100" Height="200"
Stretch="None"/>
</UserControl>
请注意,这仅适用于物理路径,因为不再涉及转换器。
您还可以尝试使用可以接受您自己的图像持有者的附加属性。
推荐阅读
- asp.net-core - Dapper 兼容性 .Net Core 3.1
- python - 试图将两个链表添加在一起但得到一个 AttributeError
- amazon-web-services - 为什么我在使用 Ansible playbook 创建 EC2 实例时收到“当前不支持请求的配置”?
- java - 什么控制 WebLogic 中的时区到 GMT 偏移映射?
- android - 可以将使用 Android Studio 制作的应用发布到 iOS 平台吗?
- c# - 如何从 WPF c# 中的 ListBox 中的 TextBox 获取所有值?
- java - 空对象引用上的 Model.Result.getFormatted_address()'
- xamarin - 将组件添加到每个视图
- python - 如何解决 ImportError: dlopen(): Symbol not found:.... Expected in: flat namespace
- encoding - 解析器未正确编码字符串