Icons from Resource Dictionary in class library are not shown in the main application window

StackOverflow https://stackoverflow.com/questions/18944586

  •  29-06-2022
  •  | 
  •  

Question

EDIT: I created a proof of concept that is much much easier:

I have a WPF application with the following MainWindow.xaml:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Image Source="pack://application:,,,/IconContainer;component/Icons/Blocked.png"/>
</Grid>

In my test solution I have have just two projects: one with the WPF application above, the other just a class .dll with a folder named Icons with a file named Blocked.png in it. The WPF application references the class library.

Nothing is shown in the Grid.

END EDIT

In my solution I have a WPF application with a ListView that shows an icon in one of its columns. At first I had these icons referenced by a ResourceDictionary directly in the WPF application and all was well. Now I am trying to move the icons to a class library and everything is falling apart.

The App.xaml:

<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:WPFBase;assembly=WPFBase" 
xmlns:local="clr-namespace:DataEditor"
xmlns:styles="clr-namespace:Styles;assembly=Styles"
StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Styles;component/Styles/BridgeItStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <styles:IconConverter x:Key="IconConverter"/>
    </ResourceDictionary>
</Application.Resources>

The MainWindow.xaml:

 <GridViewColumn.CellTemplate>
    <DataTemplate>
       <Image Source="{Binding IconName, 
              Converter={StaticResource IconConverter},ConverterParameter=S}"
       />
    </DataTemplate>
 </GridViewColumn.CellTemplate>

The Styles class library contains a ResourceDictionary containing the Style for the application, but also it contains a converter that constructs a filename for the icon that should be retrieved. This converter uses its own ResourceDictionary that contains the references to the icons.

The ResourceDictionary specifying the icons:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="ErrorL" UriSource="errorL.png"/>
    <BitmapImage x:Key="InfoL" UriSource="infoL.png"/>
    <BitmapImage x:Key="QuestionL" UriSource="questionL.png"/>
    <BitmapImage x:Key="SuccessL" UriSource="successL.png"/>
    <BitmapImage x:Key="WarnL" UriSource="warnL.png"/>
    <BitmapImage x:Key="ErrorS" UriSource="errorS.png"/>
    <BitmapImage x:Key="ErrorXS" UriSource="errorXS.png"/>
    <BitmapImage x:Key="InfoS"  UriSource="infoS.png"/>
    <BitmapImage x:Key="QuestionS" UriSource="questionS.png"/>
    <BitmapImage x:Key="SuccessS" UriSource="successS.png"/>
    <BitmapImage x:Key="WarnS" UriSource="warnS.png"/>
</ResourceDictionary>

The converter, also in the Styles class library:

Public Class IconConverter
    Implements IValueConverter

    Private _iconDictionary As ResourceDictionary

    Public Sub New()
        _iconDictionary = New ResourceDictionary()
        _iconDictionary.Source = New Uri("/Styles;component/MessageIcons/MessageIcons.xaml", UriKind.Relative)
    End Sub

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim iconName = CStr(value)
        Dim sizeParameter = CStr(parameter)
        Dim icon As BitmapImage
        Select Case iconName
            Case ProgressReport(Of Object).IconError
                Return _iconDictionary(ProgressReport(Of Object).IconError & sizeParameter)
            Case ProgressReport(Of Object).IconInfo
                icon = _iconDictionary(ProgressReport(Of Object).IconInfo & sizeParameter)
            Case ProgressReport(Of Object).IconSuccess
                Return _iconDictionary(ProgressReport(Of Object).IconSuccess & sizeParameter)
            Case ProgressReport(Of Object).IconWarn
                Return _iconDictionary(ProgressReport(Of Object).IconWarn & sizeParameter)
            Case Else
                Return Binding.DoNothing
        End Select

        Return icon
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Return Binding.DoNothing
    End Function
End Class

When I set breakpoints in the converter, I can see that the correct URI is contructed and that the icon variable is not null.

Nontheless, nothing shows up in the user-interface and no binding or other error is shown in the Immediate window of Visual Studio.

Where am I going wrong?

Was it helpful?

Solution

Estimated collegues (g.u.y.s. did not pass SO's profanity filter :-)), take heed of this, lest you will lose hours of pointless debugging like me: Set the build action of the icons to RESOURCE

There we have it...

OTHER TIPS

u r missing the attributex:Shared=False

 <BitmapImage x:Key="ErrorL" x:Shared="False" UriSource="errorL.png"/> 

hope it should solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top