共用方式為


ResourceDictionary.Source Property

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

Gets or sets a URI that provides the source location of a merged resource dictionary.

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

Syntax

'Declaration
Public Property Source As Uri
public Uri Source { get; set; }
<ResourceDictionary Source="uri"/>

Property Value

Type: System.Uri
A URI that provides the source location of a merged resource dictionary. This is typically a path that references a XAML resource within the compiled DLL, or a relative reference to a loose XAML file in the XAP.

Remarks

Typically, Source is specified as a URI that references the location of a resource dictionary.

  • If the XAML file for the ResourceDictionary to merge uses either the Page or Resource build actions, you can specify the name of the XAML file (with no leading forward slash). The internal logic that processes the Source attribute value assumes that such a reference should be resolved against the same assembly, at the base resources level of the DLL.

  • For Page or Resource build actions, you can also specify the Source attribute in a verbose form that specifies the assembly name, and uses the component; keyword to reference the resources within the assembly. For example, if your compiled application DLL is named MyApplication, and your resource XAML you want to merge is named MergedDictionary.xaml, the correct value to specify for Source is: /MyApplication;component/MergedDictionary.xaml .

  • If you are referencing a merged dictionary from a different assembly, then it is necessary to use the verbose form in order to specify that assembly's name.

  • If the XAML file for the ResourceDictionary to merge uses the Content build action, and you have not specified any additional folder structure for that content file, then you specify a leading forward slash followed by the name of the XAML file. The forward slash is resolved as relative to the originating package, and this finds the XAML as a "loose" XAML file packaged at the same root level as the DLL in the XAP package.

  • If the XAML files are project-specified or packaged within a directory structure under the root, the directory step must be included in a relative URI. For example, if you placed your XAML file brushes.xaml inside a project-defined folder BrushesAndColors, and are using the Resource build action, the source URI to use is BrushesAndColors/Brushes.xaml.

For more information about build and deployment options for merged dictionaries, see Resource Dictionaries.

Generally, the Source property is set only for a ResourceDictionary that is specified for the MergedDictionaries property (either as a property element in XAML page definitions, or as a ResourceDictionary item of the collection in code). In this context, setting the value for Source will merge the dictionary contents found at the supplied URI into the current ResourceDictionary. Any load failures will cause an exception to be thrown.

The file referenced as Source must be part of the XAP, either within the application DLL as a resource or "loose" but still in the XAP. Silverlight does not support referencing merged dictionaries outside of the application's XAP.

Building merged resource dictionaries into a separate DLL is particularly useful as a factoring technique for sharing resources across applications. For example, you might have a set of common design resources that you use in mutiple applications. A person in a designer role can produce these resources using a XAML design tool. The XAML is then used to construct a top-level ResourceDictionary, which in turn builds an assembly that packages all the resources. Each project that uses those resources can reference the assembly as a merged dictionary source. The referencing application must still package the resources assembly for deployment.

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.