首页 > 其他分享 >WPF CanFreeze,Freeze(),IsFrozen frozen clone,freeze() it is in a read-only state

WPF CanFreeze,Freeze(),IsFrozen frozen clone,freeze() it is in a read-only state

时间:2024-06-15 19:21:59浏览次数:21  
标签:frozen clone bgBrush System MainWindow Windows state using Colors

<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.

标签:frozen,clone,bgBrush,System,MainWindow,Windows,state,using,Colors
From: https://www.cnblogs.com/Fred1987/p/18249627

相关文章

  • 【仿真建模-anylogic】Statechart原理解析
    Author:赵志乾Date:2024-06-15Declaration:AllRightReserved!!!1.类图2.原理解析2.1核心函数      Statechart的核心函数如下:函数功能Statechart(Agentowner,shortmaxat)构造函数,入参指定状态图owner以及允许的最大激活Transition个数voidstart()启动函......
  • Java与数据库连接技术JDBC关键核心之PreparedStatement以及SQL注入演示解决和原理
    PreparedStatementSQL注入执行预编译的SQL对象这样也能登录成功模拟SQL注入是这个原因现在基本上不存在SQL注入的问题解决SQL注入就是传一些语句导致原来的SQL语句改变了修改代码通过设置参数的方式就能防止SQL注入实际上我们进行了一个转化将字符和关......
  • git clone github报错解决方法,亲测有效!
    报错如下:gitclonehttps://github.com/pingcap/tidb.gitCloninginto'tidb'...remote:Enumeratingobjects:331426,done.remote:Countingobjects:100%(1769/1769),done.remote:Compressingobjects:100%(1549/1549),done.error:RPCfailed;curl......
  • Spring Junit 测试报错 java.lang.IllegalStateException
    写测试代码的时候出现了java.lang.IllegalStateException:CouldnotloadTestContextBootstrapper[null].Specify@BootstrapWith's'value'attributeormakethedefaultbootstrapperclassavailable.代码如下:packagecom.example.service;importcom.example.c......
  • springboot Invalid bound statement (not found): com.elitel.xxx.dao.xxx 错误处
    如果这篇文章能给你带来帮助,不胜荣幸,如果有错误也请批评指正,一起学习,共同进步!今天给同事看了个问题,发现了这个问题,之前也遇见过,可是没有遇见这种情况,这次我记录一下。首先来说,造成这个错误的原因是什么。它是在SpringBoot应用程序中遇到“Invalidboundstatement(not......
  • SVRF Statement Syntax Conventions (SVRF 语法约束)
    SVRFStatementSyntaxConventionsSVRF语法语句约束ParameterOrder参数顺序Casesensitivity大小写敏感区分Literalkeywordsversusvariableparameters原文关键字和变量的特征Whitespaceconsiderations空白区域考虑(不太明白这个是是什么意思)Reserved......
  • Mamba: Linear-Time Sequence Modeling with Selective State Spaces
    目录概Mamba代码GuA.andDaoT.Mamba:Linear-timesequencemodelingwithselectivestatespaces.2023.概Mamba.MambaS4和S4D虽然解决了SSM计算速度的问题,但是有一个前提,就是\(A,B,C,D\)是与时间\(t\)无关的.这导致这些方法只能采取一种固定的模......
  • Hungry Hungry Hippos Towards Language Modeling with State Space Models
    目录概H3代码FuD.Y.,DaoT.,SaabK.K.,ThomasA.W.,RudraA.andReC.Hungryhungryhippos:towardslanguagemodelingwithstatespacemodels.2022.概Mamba系列第五作:H3.H3感觉H3是之前的linearattention和SSM的一个结合,它所做的只是把line......
  • 202406-如何使用新版本的rclone在服务器上挂载onedrive e5
    前情提要:这位老哥里面写的教程,因为rclone更新了所以有点不一样了,仅作记录在本地(带浏览器)操作Noremotesfound,makeanewone?n)Newremotes)Setconfigurationpasswordq)Quitconfign/s/q>nEnternamefornewremote.name>odOptionStorage.Typeofstorage......
  • git clone 私有仓库, 提示 "remote: Repository not found."
    现象私有仓库,已经在github里面关联了sshpubkey但是尝试使用gitclone将仓库克隆至本地时,提示"remote:Repositorynotfound.",如图解决方法改用AccessToken获取Github去到Setting->DeveloperSettings->personalaccesstokens->generatenewtoken......