WPF 可冻结对象Freezable冻结与解冻

参考 Freezable 对象概述
为什么了解这个可冻结对象:因为对于绘制来说 冻结可以提高性能
什么是可冻结对象
如何解冻

using System.Windows;
using System.Windows.Media;

namespace FreezableDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            var forBrush = this.TryFindResource("forBrush") as SolidColorBrush;
            if (forBrush == null) { return; }
            //forBrush.Color = Colors.Green;
            if (forBrush.IsFrozen)
            {
                var newBrush = forBrush.Clone();
                newBrush.Color = Colors.Green;
                btn.Foreground = newBrush;
            }
            else
            {
                forBrush.Color = Colors.Green;
            }
        }
    }
}
posted @ 2023-09-15 17:01  BigBosscyb  阅读(9)  评论(0)    收藏  举报