Window.Icon 屬性

定義

取得或設定視窗的圖示。

public:
 property System::Windows::Media::ImageSource ^ Icon { System::Windows::Media::ImageSource ^ get(); void set(System::Windows::Media::ImageSource ^ value); };
public System.Windows.Media.ImageSource Icon { get; [System.Security.SecurityCritical] set; }
public System.Windows.Media.ImageSource Icon { get; set; }
[<set: System.Security.SecurityCritical>]
member this.Icon : System.Windows.Media.ImageSource with get, set
member this.Icon : System.Windows.Media.ImageSource with get, set
Public Property Icon As ImageSource

屬性值

表示圖示的 ImageSource 物件。

屬性

範例

下列範例示範如何設定視窗圖示。

<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);
' Set an icon using code
Dim iconUri As New Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute)
Me.Icon = BitmapFrame.Create(iconUri)

備註

Windows Presentation Foundation (WPF) 獨立應用程式有兩種類型的圖示:

  • 其中一個元件圖示,使用 <ApplicationIcon> 應用程式專案建置檔中的 屬性來指定。 此圖示會用來做為元件的桌面圖示。

    注意

    在 Visual Studio 中偵錯時,您的圖示可能不會因為裝載程式而出現。 如果您執行可執行檔,則會出現圖示。 如需詳細資訊,請參閱裝載處理序 (vshost.exe)

  • 每個視窗一個圖示,其指定方式是設定 Icon 。 針對每個視窗,此圖示會用於標題列、其工作列按鈕,以及其 ALT-TAB 應用程式選取清單專案中。

WPF 視窗一律會顯示圖示。 設定 時, Icon WPF 會根據下列規則選擇要顯示的圖示:

  1. 如果指定,請使用元件圖示。

  2. 如果未指定元件圖示,請使用預設Microsoft Windows 圖示。

如果您使用 Icon 指定自訂視窗圖示,您可以將 設定 Iconnull 來還原預設應用程式圖示。

單一圖示可用於 Windows 的各種方式,包括顯示在視窗的標題列中、視窗的工作欄、ALT+TAB 檔案選取清單。 其中每一個都會使用不同的大小來顯示圖示;16x16 圖元圖示會顯示在視窗的標題列和工作列中,而 32x32 圖元圖示會顯示在 ALT+TAB 檔案選取清單中。 某些應用程式,例如Microsoft Windows 檔案總管,提供 [視] 功能表,可讓您選擇您想要檢視之圖示的大小。

為了符合各種顯示大小,圖示檔案是由一或多個實際圖示所組成,其中每個圖示都代表以特定大小和色彩深度為目標的圖示版本。 例如,一個圖示可能只有具有 16 個色彩的單一 16x16 圖元圖示,而另一個圖示可能包含 16x16 圖元和 32x32 圖元圖示,同時具有 16 個色彩和 256 個色彩。

如果圖示檔案中存在所有可能大小和色彩深度的圖示, Window 將會使用適當的圖示。 如果圖示檔案只包含所有可能圖示的子集, Window 請使用下一個最適當的圖示來減少大小和色彩深度。

結果是,一律會使用 Window 圖示,雖然所使用的圖示可能不是以所需的大小和色彩深度為目標。 例如,具有 16 種色彩的 16x16 圖元圖示可用於顯示為 32x32 圖元圖示,且色彩為 256 種。 這可能會導致不想要的視覺效果,例如 pixilation,但可藉由為所有目標大小和色彩深度建立圖示來避免。

注意

當視窗裝載于瀏覽器中時,您無法設定或取得此屬性。

相依性屬性資訊

識別碼欄位 IconProperty
設定為 的中繼資料屬性 true

適用於