简体   繁体   English

在 Avalonia 的应用程序资源中将图像路径设置为变量以供重用

[英]Set image path as variable in the application resources of Avalonia for re-using

I have a logo, which is used in certain places of my application.我有一个徽标,在我的应用程序的某些地方使用。 So I'd like to store it's path in a variable of my App.axaml file, which should allow me to reference this path as variable in the entire application.所以我想将它的路径存储在我的App.axaml文件的变量中,这应该允许我在整个应用程序中将此路径作为变量引用。 This works fine with colors like StepBoxBG这适用于 colors,如StepBoxBG

<Application.Resources>
    <Color x:Key="StepBoxBG">#5eba00</Color>
    <Image x:Key="LogoPath">/Assets/Logos/logo.png</Image>
</Application.Resources>

which I reference using DynamicResource in eg a border element like this我在像这样的边框元素中使用DynamicResource引用它

<Border Background="{DynamicResource StepBoxBG}" Padding="20">
...
</Border>

But when my logo path is referenced in the same way但是当我的标志路径以同样的方式被引用时

<Image Height="90" Source="{DynamicResource LogoPath}" />

no logo is displayed.不显示徽标。 The path is correct, because when I use the path directly in the Image element it works:路径是正确的,因为当我直接在Image元素中使用路径时,它可以工作:

<Image Height="90" Source="/Assets/Logos/logo.png" />

I found this question and tried it, so the App.axaml looks like this:我发现了这个问题并尝试了它,所以App.axaml看起来像这样:

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="using:My.App"
             xmlns:imaging="clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals"
             x:Class="ULabs.Image2Card.App">
    <Application.DataTemplates>
        <local:ViewLocator/>
    </Application.DataTemplates>

    <Application.Styles>
        <FluentTheme Mode="Light"/>
    </Application.Styles>

    <Application.Resources>
        <Color x:Key="StepBoxBG">#5eba00</Color>
        <imaging:Bitmap x:Key="LogoPath">
            <x:Arguments>
                <x:String>/Assets/Logos/logo.png</x:String>
            </x:Arguments>
        </imaging:Bitmap>
    </Application.Resources>
</Application>

Now it throws an exception, because it refers this as an absolute path instead of being relative to the project:现在它抛出一个异常,因为它把它称为绝对路径而不是相对于项目:

System.IO.DirectoryNotFoundException: "Could not find a part of the path 'C:\Assets\Logos\logo.png'." System.IO.DirectoryNotFoundException:“找不到路径‘C:\Assets\Logos\logo.png’的一部分。”

I set build action to AvaloniaResource so it should be included in my assembly.我将构建操作设置为AvaloniaResource ,因此它应该包含在我的程序集中。 Also tried <x:String>Assets/Logos/ul-logo.png</x:String> , now the exception refers to the debug folder ( bin/Debug/net5.0/Assets ).还尝试<x:String>Assets/Logos/ul-logo.png</x:String> ,现在异常是指调试文件夹( bin/Debug/net5.0/Assets )。

How can I specify a resource that just holds the /Assets/Logos/logo.png path and resolve it as hard-coded paths in the <Image> element would do?如何指定仅包含/Assets/Logos/logo.png路径的资源并将其解析为<Image>元素中的硬编码路径?

You cannot use a Control subclass (Image) in <Application.Resources>.您不能在 <Application.Resources> 中使用 Control 子类 (Image)。 In WPF you would usually use a BitmapImage.在 WPF 中,您通常会使用 BitmapImage。

However, BitmapImage is not available in Avalonia, but you can resort to ImageBrush as an alternative, see this great example: https://github.com/AvaloniaUI/Avalonia/issues/7211#issuecomment-998036759但是,BitmapImage 在 Avalonia 中不可用,但您可以使用 ImageBrush 作为替代方案,请参阅这个很好的示例: https://github.com/AvaloniaUI/Avalonia/issues/7211#issuecomment-998036759

Adapting the example only slightly to your use case, you could define Logo as follows in App.axaml:仅稍微调整示例以适应您的用例,您可以在 App.axaml 中定义Logo ,如下所示:

<Application.Resources>
    <Color x:Key="StepBoxBG">#5eba00</Color>
    <ImageBrush x:Key="Logo" Source="/Assets/Logos/logo.png" />
</Application.Resources>

Finally, one would refer to it in this way:最后,人们会以这种方式引用它:

<Image Height="90" Source="{Binding Source={StaticResource Logo}, Path=Source}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM