WPF CanFreeze,Freeze(),IsFrozen frozen clone,freeze() it is in a read-only state,unfreeze via deep copies include Clone() and CloneCurrenValue()

<Window x:Class="WpfApp168.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp168"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button x:Name="btn" Width="200" Height="200" Content="Freezable Btn" />
    </Grid>
</Window>



//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp168
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            SolidColorBrush bgBrush = new SolidColorBrush(Colors.Cyan);
            
            if(bgBrush.CanFreeze)
            {
                bgBrush.Freeze();
            } 
            btn.Background= bgBrush;

            try
            {
                bgBrush.Color = Colors.Red;
                //if (bgBrush.IsFrozen)
                //{
                //    SolidColorBrush newBrush = bgBrush.Clone();
                //    newBrush.Color = Colors.Red;
                //    btn.Background = newBrush;
                //}
                //else
                //{
                //    bgBrush.Color = Colors.Red;
                //}
            }
            catch(Exception ex) 
            {
                throw ex;
            }
        }
    }
}

 

Cannot set a property on object '#FF00FFFF' because it is in a read-only state.

 

 

Updated

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp168
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            SolidColorBrush bgBrush = new SolidColorBrush(Colors.Cyan);
            
            if(bgBrush.CanFreeze)
            {
                bgBrush.Freeze();
            } 
            btn.Background= bgBrush;

            try
            {
                if(bgBrush.IsFrozen)
                {
                    var newBrush = bgBrush.Clone();
                    newBrush.Color = Colors.Red;
                    btn.Background= newBrush;
                }
                else
                {
                    bgBrush.Color = Colors.Red;
                }
            }
            catch(Exception ex) 
            {
                throw ex;
            }
        }
    }
}

 

An fronzen object will quit modification and become read-only which will enhance performance via stopping monitoring changes and can be shared via multi threads and threads-safe.

posted @ 2024-06-15 19:18  FredGrit  阅读(2)  评论(0编辑  收藏  举报