共用方式為


Application.Resources Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a collection of application-scoped resources, such as styles, templates, and brushes.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Resources As ResourceDictionary
public ResourceDictionary Resources { get; set; }
<Application>
  <Application.Resources>
    oneOrMoreResourceElements
  </Application.Resources>
</Application>

XAML Values

  • oneOrMoreResourceElements
    One or more object elements, each of which creates and defines a resource. Each resource property element within each ResourceDictionary must have a unique value for the x:Key, which serves as the unique key when values are retrieved from the ResourceDictionary.

Property Value

Type: System.Windows.ResourceDictionary
A ResourceDictionary object that contains zero or more application-scoped resources.

Remarks

The value of the Resources property contains the ResourceDictionary that is set from XAML by using the Application.Resources property element.

Examples

The following example shows a style (textBlockStyle) declared as an application-scoped resource that is shared across multiple UIs.

Run this sample

First, the resource is implemented in the markup for the application class, as shown here.

<Application 
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SilverlightApplication.App"
    Startup="App_Startup">

    <Application.Resources>
        <Style x:Key="textBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </Application.Resources>

</Application>

The following UIs demonstrate the application-scoped textBlockStyle being shared.

<UserControl 
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication"
    x:Class="SilverlightApplication.Page">

    <StackPanel>
        <TextBlock Style="{StaticResource textBlockStyle}">
            This text is red!
        </TextBlock>
        <local:AnotherPage />
    </StackPanel>

</UserControl>
<UserControl 
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SilverlightApplication.AnotherPage">

    <StackPanel>
        <TextBlock Style="{StaticResource textBlockStyle}">
            This text is red too!
        </TextBlock>
    </StackPanel>

</UserControl>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.