2

我想从 dll 设置我的窗口图标,但它的抛出错误。

我在 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: 
4

1 回答 1

4

尝试以下方法:

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

为此,必须将图标构建为Resource

于 2012-04-28T01:05:03.160 回答