所有应用程序窗口的 WPF 图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1881580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:18:57  来源:igfitidea点击:

WPF Icon for all app windows

wpficonswindow

提问by vts123

It is possible to set one Icon so, that it would be used on every window in current app. So that i set it once (not on every window by hand)..?

可以设置一个图标,以便在当前应用程序的每个窗口上使用它。这样我就设置了一次(不是在每个窗口上手动设置)..?

回答by jsmith

A good reference on the subject is here MSDN. States that you have an Icon for the Application (Desktop Icon), and one for each Window.

关于这个主题的一个很好的参考是这里MSDN。说明您有一个应用程序图标(桌面图标),每个窗口都有一个图标。

A WPF window always displays an icon. When one is not provided by setting Icon, WPF chooses an icon to display based on the following rules:

WPF 窗口始终显示一个图标。当没有通过设置 Icon 提供时,WPF 根据以下规则选择要显示的图标:

  1. Use the assembly icon, if specified.

  2. If the assembly icon is not specified, use the default Microsoft Windows icon.

  1. 如果指定,请使用程序集图标。

  2. 如果未指定程序集图标,请使用默认的 Microsoft Windows 图标。

Community Content Reference:

社区内容参考:

"A liitle tip : if you set the application icon and expect to see it on the window - it wont show up if running in debug from VS. Running externally or without attaching (ctrl + f5) the icon displays as expected."

“一个小提示:如果您设置应用程序图标并希望在窗口上看到它 - 如果在 VS 的调试中运行它就不会显示。在外部运行或不附加 (ctrl + f5) 图标会按预期显示。”

回答by John Myczek

Set the icon in the project properties on the "Application" tab in the "Resources" section. This icon will be the default icon for all windows in the application.

在“资源”部分的“应用程序”选项卡上的项目属性中设置图标。此图标将成为应用程序中所有窗口的默认图标。

回答by DaveCleland

Under VS2010 open the Properties for the main application executable and open the Application tab. Set the icon under 'Icon and Manifest' in the Resources section.

在 VS2010 下,打开主应用程序可执行文件的属性并打开应用程序选项卡。在资源部分的“图标和清单”下设置图标。

To see the icon while debugging under VS2010 you need to open the Debug tab and uncheck the option for 'Enable the Visual Studio hosting process', otherwise you will only see the default icon on most windows.

要在 VS2010 下调试时查看图标,您需要打开调试选项卡并取消选中“启用 Visual Studio 托管进程”选项,否则您将只能在大多数窗口中看到默认图标。

I assume that the icon loading code is getting confused by the hosting process and is looking in "someapplication.vshost.exe" instead of "someapplication.exe" for the icons.

我假设图标加载代码被托管进程弄糊涂了,并且正在“someapplication.vshost.exe”而不是“someapplication.exe”中查找图标。

This looks like it's fixed in VS2013.

这看起来像是在 VS2013 中修复的。

回答by Frosty

The reason that "Enable the Visual Studio hosting process" makes the icon not work is that it is started using the vshost.exe, and thereby the manifest is not read properly. The same goes if you have other stuff in the manifest, like regfree ocx controls etc that requires the manifest to load.

“启用 Visual Studio 托管进程”使图标不起作用的原因是它是使用 vshost.exe 启动的,因此无法正确读取清单。如果清单中有其他内容,例如需要加载清单的 regfree ocx 控件等,情况也是如此。

回答by Nadeem Shaikh

You can also try this to set your own icon:

你也可以试试这个来设置你自己的图标:

private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            Uri iconUri = new Uri(@"C:\Apps\R&D\WPFNavigation\WPFNavigation\Images\airport.ico", UriKind.RelativeOrAbsolute);
            (this.Parent as Window).Icon = BitmapFrame.Create(iconUri);
        }