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 @   FredGrit  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2022-06-15 C++ mysql read table
2022-06-15 MySQL get table size
2020-06-15 C# StreamWriter log batch message sync and async
点击右上角即可分享
微信分享提示