NtscCobalt
NtscCobalt

Reputation: 1689

Image and Control in external class library

So I have an application and a class library.

The class library has

UserControls/Test.xaml
Images/TestImage.png

Test image is marked as 'Content' and 'Copy to Output Directory' is set to false because I want all of my images to be stored inside the class dll.

Test.xaml looks something like this...

<UserControl x:Class="TestLib.UserControls.Test"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         >
         <Image Source="/TestLib;component/Images/TestImage.png"/>
</UserControl>

The designer works fine but when I try to instantiate the UserControl/Test.xaml in my application then it throws the following exception:

System.IO.IOException occurred
Message=Cannot locate resource 'images/TestImage.png'.
Source=PresentationFramework
StackTrace:
   at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
InnerException: 

I have tried changing the resource type to "Resource" and "Embedded Resource" but neither makes any difference. I have also tried using the full pack string

"pack://application,,,/TestLib;component/Images/TestImage.png" 

but it throws the exact same exception even stating the same 'images/TestImage.png'.

Edit: I forgot to mention that Images/TestImage.png is not "included" it is "included as link". Apparently this is the problem because of a compiler bug: Linked Files within a Folder Structure

Upvotes: 1

Views: 2087

Answers (2)

NtscCobalt
NtscCobalt

Reputation: 1689

It looks like the solution is to not use "linked" instead of "included" resources within libraries. The compiler fails to respect their path which makes them impossible to reference when using them inside of a class library. See: Linked Files within a Folder Structure

Even if the linked file lives on the root of a project you still cannot reference it at runtime.

Upvotes: 0

Pavel Korsukov
Pavel Korsukov

Reputation: 959

The last URI should work with 'Resource' build action. Try code from this answer.

Upvotes: 1

Related Questions