AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.1k stars 2.26k forks source link

<AvaloniaResource Include="..\assets\**" /> doesn't work #16246

Open logixoul opened 4 months ago

logixoul commented 4 months ago

Describe the bug

Relative paths in the AvaloniaResource tag don't seem to work.

To Reproduce

I have this filesystem structure: -- MySolution/ --- assets/ ---- logo.png --- MyProject/ ---- MyProject.csproj

I added this to my csproj file:

    <ItemGroup>
        <AvaloniaResource Include="..\assets\**" />
    </ItemGroup>

... and this to MainWindow.axaml:

<Image Margin="20" Height="200" Width="200" Source="assets/logo.png"/>

Got a System.IO.FileNotFoundException.

When I move the assets dir into the MyProject dir and change the AvaloniaResource line to say "assets**", things work fine. Maybe I'm doing something wrong, but I think this is a bug.

Expected behavior

No response

Avalonia version

11.0.10

OS

Windows

Additional context

No response

Eugenenoble2005 commented 4 months ago

Did you try using the full rooted path? Maybe something like:

<Image Margin="20" Height="200" Width="200" Source="../assets/logo.png"/>

Or the relative path:

<Image Margin="20" Height="200" Width="200" Source="../logo.png"/>
Or perhaps
<Image Margin="20" Height="200" Width="200" Source="logo.png"/>
logixoul commented 4 months ago

@Eugenenoble2005 yep, to no avail.

Eugenenoble2005 commented 4 months ago

@Eugenenoble2005 yep, to no avail.

Maybe avalonia can't reach it from outside the project. If you really need it outside the project, try creating a shared class library, put the assets there and try to access the resource via the assembly name

<Image Source="avares://sharedAssembly/Assets/icon.png"/>
maxkatz6 commented 4 months ago

Please use Link msbuild attribute to assign more compiler-friendly path to your assets:

    <ItemGroup>
        <AvaloniaResource Include="../assets/**"
                                  Link="assets/%(RecursiveDir)%(FileName)%(Extension)"  />
    </ItemGroup>
maxkatz6 commented 4 months ago

Once we support LinkBase, it will be easier as well https://github.com/AvaloniaUI/Avalonia/pull/16118