Bewerken

Delen via


x:Shared Attribute

When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

XAML Attribute Usage

<ResourceDictionary>
  <object x:Shared="false".../>
</ResourceDictionary>

Remarks

x:Shared is mapped to the XAML language XAML namespace and is recognized as a valid XAML language element by .NET XAML Services and its XAML readers. However, the stated capabilities of x:Shared are only relevant for WPF applications and for the WPF XAML parser. In WPF, x:Shared is only useful as an attribute when it is applied to an object that exists within a WPF ResourceDictionary. Other usages do not throw parse exceptions or other errors, but they have no effect.

The meaning of x:Shared is not specified in the XAML language specification. Other XAML implementations, such as those that build on .NET XAML Services, do not necessarily provide resource-sharing support. Such XAML implementations could provide similar behavior in the supporting framework that also used x:Shared values.

In WPF, the default x:Shared condition for resources is true. This condition means that any given resource request always returns the same instance.

Modifying an object that is returned through a resource API, such as FindResource, or modifying an object directly within a ResourceDictionary, changes the original resource. If references to that resource were dynamic resource references, the consumers of that resource get the changed resource.

If references to the resource were static resource references, changes to the resource after XAML processing time are irrelevant. For more information about static versus dynamic resource references, see Overview of XAML resources (WPF .NET).

Explicitly specifying x:Shared="true" is rarely done, because that is already the default. There is no direct code equivalent for x:Shared in the WPF object model; it can only be specified in a XAML usage and must be processed either by the default WPF behavior or in an intermediate XAML node stream on the load path if processed using .NET XAML Services and its XAML readers.

A scenario for x:Shared="false" is if you define a FrameworkElement or FrameworkContentElement derived class as a resource and then you introduce the element resource into a content model. x:Shared="false" enables an element resource to be introduced multiple times in the same collection (such as a UIElementCollection). Without x:Shared="false" this is invalid because the collection enforces uniqueness of its contents. However, the x:Shared="false" behavior creates another identical instance of the resource instead of returning the same instance.

Another scenario for x:Shared="false" is if you use a Freezable resource for animation values but want to modify the resource on a per animation basis.

The string handling of false is not case sensitive.

In WPF, x:Shared is only valid under the following conditions:

See also