Export (0) Print
Expand All

x:Shared Attribute

When set to false, modifies Windows Presentation Foundation (WPF) resource retrieval behavior such that requests for a resource will create a new instance for each request, rather than sharing the same instance for all requests.

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

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 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 will get the changed resource. (If references to the resource were static resource references, changes to the resource after XAML load time are irrelevant. For details on static versus dynamic resource references, see Resources Overview.)

Specifying x:Shared="true" is uncommon, because that is already the default. There is no direct code equivalent for x:Shared.

One scenario for x:Shared="false" is if you define a FrameworkElement or FrameworkContentElement derived class as a resource and 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 would be illegal, because the collection enforces uniqueness of its contents. But the x:Shared="false" behavior basically creates another identical instance of the resource, rather than 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.

x:Shared is only legal under the following conditions:

  • The ResourceDictionary that contains the items with x:Shared must be compiled. The ResourceDictionary cannot be within loose XAML or used for themes.

  • The ResourceDictionary that contains the items must not be nested within another ResourceDictionary. For instance, you cannot use x:Shared for items in a ResourceDictionary that is within a Style that is already a ResourceDictionary item.

Community Additions

ADD
Show:
© 2014 Microsoft