更改WPF窗口图标

I have a bunch of icons as resources. Managed to change MainWindow icon without issues, but doing the same thing on others gives me this error:

Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\resources\add.ico'

我在XAML文件的顶部中具有以下内容:

Icon="resources/add.ico"

So - for some reason it searches for resources in resources folder when in MainWindow.xaml, but in other window it decides to search in VS folder (for unknown reason). How can i fix this?

评论
  • 逗妇乳
    逗妇乳 回复

    If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

    To do this, create a BitmapSource as a resource somewhere:

    <BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

    然后,在您的代码中,使用类似以下内容的代码:

    <Image Source="{StaticResource MyImageSource}" />

    In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

    For more information look HERE

  • 没文化
    没文化 回复

    我建议您使用WPF Pack-URI使用绝对路径。 这些帖子对此进行了很好的解释:

    https://stackoverflow.com/a/2416464/10724593
    https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/aa970069(v=vs.100)?redirectedfrom=MSDN

    For you it would be pack://application:,,,/resources/add.ico

    The basic syntax for it is pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml as stated on the MSDN Article