x:Shared Attribute in xaml (2)
Document about x:Shared Attribute on MSDN:
https://msdn.microsoft.com/en-us/library/aa970778.aspx
The default value for x:Shared is true. It means that any given resource request always returns the same instance.
In the preview blog, the x:Shared property of Image control resource is true. So the Content of ContentControl is pointed to the same Image control.
<Border Background="Blue" Margin="10">
<ContentControl Content="{StaticResource image}"/>
</Border>
But the Image control has only one Parent in Logic Tree. So it just shows in the last ContentControl.
<TextBlock Text="Third"/>
<ContentControl Height="150"
ContentTemplate="{DynamicResource dataTemplate}"/>
But if set the x:Shared=”false” for Image control, every time requesting the image resource, it will create a new Instance Image control. So, it will show 3 images.
From the MSDN, it explains there are two scenarios using x:Shared=”false”
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.
In my application, it’s the first scenario.
Comments
- Anonymous
December 25, 2008
PingBack from http://www.codedstyle.com/xshared-attribute-in-xaml-2/