编辑: 我创建了一个概念证明是非常非常简单:图标在主应用程序窗口不显示
我有以下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与一个在其名为Blocked.png文件的文件夹命名为图标。 WPF应用程序引用类库。
网格中没有显示任何内容。
编辑完
在我的解决方案我有一个ListView,显示在其列一个图标的WPF应用程序。起初,我在WPF应用程序中直接通过ResourceDictionary引用了这些图标,并且一切正常。现在我正试图将图标移动到类库中,并且一切都崩溃了。
的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>
的MainWindow.xaml:
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding IconName,
Converter={StaticResource IconConverter},ConverterParameter=S}"
/>
</DataTemplate>
</GridViewColumn.CellTemplate>
的样式类库包含包含应用程序样式ResourceDictionary中,还包含一个为应该检索的图标构造文件名的转换器。此转换器使用其自己的ResourceDictionary,其中包含对图标的引用。
的ResourceDictionary中指定的图标:
<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
当我在转换器设置断点,我可以看到正确的URI是被构造并且图标变量不为空。
但是,用户界面中没有显示任何内容,Visual Studio的“立即”窗口中未显示任何绑定或其他错误。
我哪里错了?
你在类库或主应用程序中有图标? – Nitin
在类库中,这是关键:-) – Dabblernl
rt ..你可以尝试更新UriSource的uri值,如pack:// application:,,,/ReferencedAssembly; component/icon.png – Nitin