如何使用系統資源 (WPF .NET)

此範例示範如何使用系統定義的資源。 系統資源是由 WPF 提供,並允許存取作業系統資源,例如字型、色彩和圖示。 系統資源會將數個系統定義的值公開為資源和屬性,以協助您建立與 Windows 一致的視覺效果。

重要

.NET 7 和 .NET 6 的桌面指南檔正在建置中。

字型

使用 類別 SystemFonts 來參考作業系統所使用的字型。 這個類別包含系統字型值做為靜態屬性,以及參考資源索引鍵的屬性,這些索引鍵可用來在執行時間動態存取這些值。 例如, CaptionFontFamily 是值 SystemFonts ,而 CaptionFontFamilyKey 是對應的資源索引鍵。

下列範例示範如何存取並使用 的屬性 SystemFonts 做為靜態值來設定或自訂文字區塊:

<TextBlock FontSize="{x:Static SystemFonts.SmallCaptionFontSize}"
           FontWeight="{x:Static SystemFonts.SmallCaptionFontWeight}"
           FontFamily="{x:Static SystemFonts.SmallCaptionFontFamily}"
           Text="Small Caption Font">
</TextBlock>

若要在程式碼中使用 的值 SystemFonts ,您不需要使用靜態值或動態資源參考。 請改用 類別的非 SystemFonts 索引鍵屬性。 雖然非索引鍵屬性顯然定義為靜態屬性,但系統所裝載的 WPF 執行時間行為會即時重新評估屬性,並適當地考慮使用者導向的系統值變更。 下列範例示範如何指定按鈕的字型設定:

var myButton = new Button()
{
    Content = "SystemFonts",
    Background = SystemColors.ControlDarkDarkBrush,
    FontSize = SystemFonts.IconFontSize,
    FontWeight = SystemFonts.MessageFontWeight,
    FontFamily = SystemFonts.CaptionFontFamily
};

mainStackPanel.Children.Add(myButton);
Dim myButton = New Button() With
{
    .Content = "SystemFonts",
    .Background = SystemColors.ControlDarkDarkBrush,
    .FontSize = SystemFonts.IconFontSize,
    .FontWeight = SystemFonts.MessageFontWeight,
    .FontFamily = SystemFonts.CaptionFontFamily
}

mainStackPanel.Children.Add(myButton)

XAML 中的動態字型

系統字型度量資訊可以當成靜態或動態資源使用。 如果您想要讓字型計量在應用程式執行時自動更新,請使用動態資源;否則,請使用靜態資源。

注意

動態資源會將 關鍵字 Key 附加至屬性名稱。

下列範例示範如何存取和使用系統字型動態資源來設定或自訂文字區塊的樣式:

<TextBlock FontSize="{DynamicResource {x:Static SystemFonts.SmallCaptionFontSize}}"
           FontWeight="{DynamicResource {x:Static SystemFonts.SmallCaptionFontWeight}}"
           FontFamily="{DynamicResource {x:Static SystemFonts.SmallCaptionFontFamily}}"
           Text="Small Caption Font">
</TextBlock>

參數

使用 類別 SystemParameters 參考系統層級屬性,例如主要顯示器的大小。 這個類別同時包含系統參數值屬性,以及系結至值的資源索引鍵。 例如, FullPrimaryScreenHeightSystemParameters 屬性值,而 FullPrimaryScreenHeightKey 是對應的資源索引鍵。

下列範例示範如何存取和使用 的 SystemParameters 靜態值來設定或自訂按鈕的樣式。 此標記範例會將值套用 SystemParameters 至按鈕來調整按鈕的大小:

<Button FontSize="8" 
        Height="{x:Static SystemParameters.CaptionHeight}"
        Width="{x:Static SystemParameters.IconGridWidth}"
        Content="System Parameters">
</Button>

若要在程式碼中使用 的值 SystemParameters ,您不需要使用靜態參考或動態資源參考。 請改用 類別的值 SystemParameters 。 雖然非索引鍵屬性顯然定義為靜態屬性,但系統所裝載的 WPF 執行時間行為會即時重新評估屬性,並適當地考慮使用者導向的系統值變更。 下列範例示範如何使用 值來設定按鈕 SystemParameters 的寬度和高度:

var myButton = new Button()
{
    Content = "SystemParameters",
    FontSize = 8,
    Background = SystemColors.ControlDarkDarkBrush,
    Height = SystemParameters.CaptionHeight,
    Width = SystemParameters.CaptionWidth,
};

mainStackPanel.Children.Add(myButton);
Dim myButton = New Button() With
{
    .Content = "SystemParameters",
    .FontSize = 8,
    .Background = SystemColors.ControlDarkDarkBrush,
    .Height = SystemParameters.CaptionHeight,
    .Width = SystemParameters.CaptionWidth
}

mainStackPanel.Children.Add(myButton)

XAML 中的動態參數

系統參數度量資訊可以當成靜態或動態資源使用。 如果您想要參數計量在應用程式執行時自動更新,請使用動態資源;否則,請使用靜態資源。

注意

動態資源會將 關鍵字 Key 附加至屬性名稱。

下列範例示範如何存取及使用系統參數動態資源,以設定或自訂按鈕的樣式。 這個 XAML 範例會將值指派 SystemParameters 給按鈕的寬度和高度,以調整按鈕的大小。

<Button FontSize="8" 
        Height="{DynamicResource {x:Static SystemParameters.CaptionHeightKey}}"
        Width="{DynamicResource {x:Static SystemParameters.IconGridWidthKey}}"
        Content="System Parameters">
</Button>

另請參閱