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;
}
}
}
}

浙公网安备 33010602011771号