Page 1 of 1

Are there any benefits of freezing objects in resources?

Posted: 06 Dec 2023, 12:26
by AnKor
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:
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" p:Freeze="True"
Does it work or provide any benefits in Noesis?

Re: Are there any benefits of freezing objects in resources?

Posted: 11 Dec 2023, 12:48
by sfernandez
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.
<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?

Posted: 11 Dec 2023, 21:39
by AnKor
Thank you! This is very useful info.