AnKor
Topic Author
Posts: 35
Joined: 22 Jul 2022, 16:14

Are there any benefits of freezing objects in resources?

06 Dec 2023, 12:26

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?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

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

11 Dec 2023, 12:48

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>
 
AnKor
Topic Author
Posts: 35
Joined: 22 Jul 2022, 16:14

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

11 Dec 2023, 21:39

Thank you! This is very useful info.

Who is online

Users browsing this forum: No registered users and 5 guests