类库中资源字典中的图标不会显示在主应用程序窗口中



>编辑:我创建了一个更容易的概念证明:

我有一个带有以下 MainWindow.xaml 的 WPF 应用程序:

<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>

在我的测试解决方案中,我只有两个项目:一个包含上面的 WPF 应用程序,另一个只是一个类.dll,其中包含一个名为 Icons 的文件夹,其中包含一个名为 Blocked.png 的文件。WPF 应用程序引用类库。

网格中不显示任何内容。

结束编辑

在我的解决方案中,我有一个带有 ListView 的 WPF 应用程序,该应用程序在其一列中显示一个图标。起初,我让资源字典直接在 WPF 应用程序中引用这些图标,一切都很好。现在我正在尝试将图标移动到类库中,但一切都分崩离析。

应用程序:

<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>

类库包含一个包含应用程序样式的资源字典,而且还包含一个转换器,该转换器为应检索的图标构造文件名。此转换器使用自己的资源字典,其中包含对图标的引用。

指定图标的资源字典:

<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>

转换器,也在 Styles 类库中:

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

当我在转换器中设置断点时,我可以看到构造了正确的 URI,并且图标变量不为 null。

当然,用户界面中不会显示任何内容,Visual Studio 的"即时"窗口中也不会显示绑定或其他错误。

我哪里出错了?

估计的同事(g.u.y.s.没有通过SO的亵渎过滤器:-)),请注意这一点,以免像我一样浪费数小时的毫无意义的调试:将图标的生成操作设置为"资源"

我们有它...

您缺少属性x:Shared=False

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

希望它能解决你的问题。

最新更新