c# - 从 WPF 中的 dll 设置窗口图标

标签 c# wpf xaml

我想从 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: 

最佳答案

尝试以下操作:

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

为此,图标必须构建为资源

关于c# - 从 WPF 中的 dll 设置窗口图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10357169/

相关文章:

c# - 带有画笔的 Oxyplot 注释

c# - 远程服务器返回错误: (401) Unauthorized Error occured when i try to download file via Web Client

c# - 从串口读取数据

c# - 如何引用要在即时或快速监视中使用的 namespace ?

c# - 如何在 WPF 中构建 "word-by-word"自动完成?

c# - 如何在 WPF 中隐藏 GridViewColumn,就好像它在运行时折叠一样?

c# - 如何将 ascii 字节向量转换为字符串?

wpf - 如何在WPF选项卡控件中创建梯形选项卡

WPF:如何在WPF/MVVM中制作Google Chrome样式的GUI?

c# - 如何以编程方式将 C# 中的图像源设置为 XAML 静态资源?