Image Resource in referenced Class Library shows up in designer but not at run time

Franklin Cole 6 Reputation points
2022-03-01T13:10:15.313+00:00

I have a lot of images I want to use in an application I'm working on. However, the more I add as a resource, the slower Visual Studio gets. I assume this is because it's due to recompiling with all of the resources in the same project. So I was hoping to add them to a separate class library and use them from there. It works in the designer, however the image does not appear during run time.

The image is in a class library named Icons, with a subfolder of Icons, and in this case a smiley.png file. The smiley.png Build Action has been set to Resource within the Icons project.

<Project Sdk="Microsoft.NET.Sdk">  
  
        <PropertyGroup>  
            <TargetFramework>netcoreapp3.1</TargetFramework>  
        </PropertyGroup>  
  
        <ItemGroup>  
            <None Remove="Icons\smiley.png" />  
        </ItemGroup>  
  
        <ItemGroup>  
            <Resource Include="Icons\smiley.png" />  
        </ItemGroup>  
  
    </Project>  

The main project is a WPF Application (.NET Core 3.1) called Test. In the MainWindow.xaml I just have the starter grid and an image.

    <Grid>  
        <Image Source="/Icons;component/Icons/smiley.png" Width="450" />  
    </Grid>  

Test is also referencing icons.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  
  
  <PropertyGroup>  
    <OutputType>WinExe</OutputType>  
    <TargetFramework>netcoreapp3.1</TargetFramework>  
    <UseWPF>true</UseWPF>  
  </PropertyGroup>  
  
  <ItemGroup>  
    <ProjectReference Include="..\Icons\Icons.csproj" />  
  </ItemGroup>  
  
</Project>  
  

To be clear I want to use the icons as a resource from a class library. Not as content copied to the build directory. I have also attached images just in case I wasn't clear on something. I have also tried with both relative and absolute URIs, the result is the same.

I've tried everything I can think of at this point. It works if the image is in the main project as a resource, but not when it's in a referenced assembly. Though according to the docs this should work. I'm stumped as to why it isn't.

178769-designer.png
178880-result.png
178925-absolute-and-relative.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,401 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
772 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Franklin Cole 6 Reputation points
    2022-03-01T14:23:22.743+00:00

    Just incase some one else has this problem in the future I found the answer on stackoverflow.

    I fixed it by changing the class library project's Sdk property from:

    <Project Sdk="Microsoft.NET.Sdk">  
    

    to

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  
    

    If anyone could explain why that fixes it it would be greatly appreciated.

    178933-sdk-windowsdesktop.png

    0 comments No comments

  2. Karen Payne MVP 35,201 Reputation points
    2022-03-02T02:57:27.023+00:00

    In regards to <Project Sdk="Microsoft.NET.Sdk"> vs <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> you can find the answer here. It is dependent on the Framework used.

    0 comments No comments