Share via


x:Shared Attribute in xaml (3)

If want to share Image in ResourceDictionary, there is another way except using x:Shared=”false”.

 <Application.Resources>
    <BitmapImage x:Key="bitmapImage"  
      UriSource="pack://application:,,,/Resource/Dock.JPG"/>
    
    <DataTemplate x:Key="dataTemplate" x:Shared="false">
        <Border Background="Blue" Margin="10">
            <Image Source="{StaticResource bitmapImage}"/>
        </Border>
    </DataTemplate>
</Application.Resources>

Instead of creating Image as resource, create BitmapImage as resource and use as the Source of Image in DataTemplate.

Comparing it with using x:Shared=”false”, it will create 3 Image control instances but only one BitmapImage control instance.

But if we use x:Shared=”false”, it will create 3 Image control instances and 3 BitmapFrameDecode instance.

Comments