2011-02-24 150 views

回答

25

Window的Icon属性用于设置窗口的图标。

<Window x:Class="WindowSample.MainWindow" 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

Title="WPF Window Sample" Height="350" Width="525" 

Name="FirstWindow" Icon="Icon1.ico" > 

Window类的Icon属性表示运行时窗口的图标。该属性需要一个ImageSource变量。

以下代码片段使用BitmapFrame.Create方法创建ImageSource并设置Window的Icon属性。

Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute); 

this.Icon = BitmapFrame.Create(iconUri); 

你可以阅读更多从here

7
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="SDKSample.MainWindow" 
Icon="WPFIcon1.ico"> 
</Window> 

或代码

// Set an icon using code 
Uri iconUri = new Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute); 
this.Icon = BitmapFrame.Create(iconUri); 

来源:Window.Icon Property

1
  1. 添加ICO文件到项目资源和检查作为嵌入的资源
  2. 套装项目 - >属性 - >图标发布模式或开始从资源
  3. 运行项目选择没有调试
+0

虽然这几个步骤对我来说是工作的,但我仍然困惑为什么在调试中显示默认图标。 – Tiny 2015-09-07 13:15:31

+1

这些步骤在visual studio的上下文中没有任何意义。你如何添加一个图标来投影“资源”?你参考的这个“属性 - >图标”菜单在哪里?等 – Jake 2016-10-04 20:11:46

+0

R单击解决方案资源管理器菜单>属性>应用程序>资源中的项目 – Mike 2017-10-16 17:40:30

6

简单的方法来添加图片到标题栏:

在你的项目, 选择 - 属性 - 应用程序 - 资源 - 图标清单 - 选择.ICO图像(总是转换您的图像。 ICO)

在WPF主窗口中加入这一行(图标):

Title="xxxxx" **Icon="xxxxxx.ico"**> 
+0

R单击解决方案资源管理器菜单中的项目>属性>应用程序>资源 – Mike 2017-10-16 17:40:45

0
<Window Icon="youricon.ico"></Window>