Are there any benefits of freezing objects in resources?
To be honest I don't fully understand how Freezable works, but some people recommend freezing objects which are placed in a resource dictionary.
This can even be done directly in XAML like this:
Does it work or provide any benefits in Noesis?
This can even be done directly in XAML like this:
Code: Select all
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" p:Freeze="True"-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Are there any benefits of freezing objects in resources?
Frozen resources have a small performance improvement, as they won't change, objects that use them won't be listening for changes.
By default, all freezable resources placed in the Application Resources will be automatically frozen, so you don't need to worry about that.
Another benefit/feature that frozen objects provide is for example when you use a resource in a template and animate it. Frozen resources will create a copy for each instance of the control and animate that. Non frozen resources won't be cloned when animated, so animation will affect all control instances.
By default, all freezable resources placed in the Application Resources will be automatically frozen, so you don't need to worry about that.
Another benefit/feature that frozen objects provide is for example when you use a resource in a template and animate it. Frozen resources will create a copy for each instance of the control and animate that. Non frozen resources won't be cloned when animated, so animation will affect all control instances.
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<Grid.Resources>
<SolidColorBrush x:Key="br1" Color="Red"/>
<SolidColorBrush x:Key="br2" Color="Red" p:Freeze="True"/>
<ControlTemplate x:Key="t1" TargetType="Button">
<Border x:Name="bd" Background="{TemplateBinding Background}" Padding="20,10" CornerRadius="4">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Cyan" Storyboard.TargetName="bd" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Red" Storyboard.TargetName="bd" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Grid.Resources>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel>
<Button Content="Button1" Margin="10" Background="{StaticResource br1}" Template="{StaticResource t1}"/>
<Button Content="Button2" Margin="10" Background="{StaticResource br1}" Template="{StaticResource t1}"/>
</StackPanel>
<StackPanel>
<Button Content="Button1" Margin="10" Background="{StaticResource br2}" Template="{StaticResource t1}"/>
<Button Content="Button2" Margin="10" Background="{StaticResource br2}" Template="{StaticResource t1}"/>
</StackPanel>
</StackPanel>
</Grid>Re: Are there any benefits of freezing objects in resources?
Thank you! This is very useful info.
Who is online
Users browsing this forum: No registered users and 5 guests