如何通过 C# 将 WPF 窗口的图标设置为图标文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12392790/
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-13 05:19:30  来源:igfitidea点击:

How to set a WPF window's icon to an icon file through C#?

c#wpficonsmainwindow

提问by IneedHelp

I have a WPF project with a window. If I want to set the window's icon property in C#, it allow me to feed it only an ImageSource and it doesn't accept icon files. If I set the property in XAML, it accepts icon files without any problem. How can I set the a WPF Window's icon to an icon file through C# code?

我有一个带窗口的 WPF 项目。如果我想在 C# 中设置窗口的图标属性,它允许我只提供一个 ImageSource 并且它不接受图标文件。如果我在 XAML 中设置该属性,它会毫无问题地接受图标文件。如何通过 C# 代码将 WPF 窗口的图标设置为图标文件?

回答by Nikhil Agrawal

How about

怎么样

mywindow.Icon = new BitmapImage(new Uri(@"C:\myicon.ico"));