在WPF中从dll设置窗口图标

时间:2012-04-27 20:08:21

标签: c# wpf xaml

我想从一个dll设置我的Window图标,但是它的抛出错误。

我在JIMS.Resources.dll中有一个名为JIMS.ico的图标,我在我的窗口中将其消耗为

<Window x:Class="JIMS.Home"        
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                  
        Title="JIMS" WindowStyle="None" 
        Icon="/JIMS.Resources;component/JIMS.ico">
</Window>

但它抛出异常

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '4' and line position '145'.
  Source=PresentationFramework
  LineNumber=4
  LinePosition=145
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at JIMS.Home.InitializeComponent() in d:\JIMS\JIMS\Home.xaml:line 1
       at JIMS.Home..ctor() in D:\JIMS\JIMS\Home.xaml.cs:line 25
  InnerException: System.IO.IOException
       HResult=-2146232800
       Message=Cannot locate resource 'jims.ico'.
       Source=PresentationFramework
       StackTrace:
            at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
            at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
            at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
            at System.IO.Packaging.PackWebResponse.GetResponseStream()
            at System.IO.Packaging.PackWebResponse.get_ContentType()
            at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
            at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
            at System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy)
            at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
            at System.Windows.Baml2006.TypeConverterMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
       InnerException: 

1 个答案:

答案 0 :(得分:3)

尝试以下方法:

Icon="pack://application:,,,/JIMS.Resources;component/JIMS.ico"

这样,图标必须构建为Resource