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

Wpf 类库中资源字典中的图标不会显示在主应用程序窗口中,wpf,resourcedictionary,Wpf,Resourcedictionary,编辑: 我创建了一个更简单的概念证明: 我有一个具有以下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"&

编辑: 我创建了一个更简单的概念证明:

我有一个具有以下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>
 <GridViewColumn.CellTemplate>
    <DataTemplate>
       <Image Source="{Binding IconName, 
              Converter={StaticResource IconConverter},ConverterParameter=S}"
       />
    </DataTemplate>
 </GridViewColumn.CellTemplate>
当我在转换器中设置断点时,我可以看到构造了正确的URI,并且icon变量不是null

否则,用户界面中不会显示任何内容,VisualStudio的即时窗口中也不会显示绑定或其他错误


哪里出错了?

u r缺少属性
x:Shared=False

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

希望它能解决您的问题。

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


在类库或主应用程序中有图标?在类库中,这就是要点:-)rt。。可以尝试将UriSource更新为uri值吗pack://application:,,,/ReferencedAssembly;component/icon.pngI已经尝试过了,不起作用。。。我已经创建了一个概念证明。很快就会贴出来。更容易理解。如果你真的不会说“伙计”的话就测试。WFM(padpad)
<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>
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
 <BitmapImage x:Key="ErrorL" x:Shared="False" UriSource="errorL.png"/>