gpt4 book ai didi

wpf - 从Freezable派生的WPF对象如何在XAML中卡住?

转载 作者:行者123 更新时间:2023-12-03 09:18:56 28 4
gpt4 key购买 nike

WPF中的许多类型都来自Freezable。它提供了可变POCO对象的不变性,并在某些情况下提高了性能。

所以我的问题是,如何冻结XAML标记中的对象?

(请注意,我也张贴了similar but different question)。

最佳答案

要冻结标记中声明的Freezable对象,请使用XML namespace Freeze中定义的http://schemas.microsoft.com/winfx/2006/xaml/presentation/options属性。

在以下示例中,SolidColorBrush声明为页面资源并被冻结。然后用于设置按钮的背景。

<Page 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="po">

<Page.Resources>
<!-- This brush is frozen -->
<SolidColorBrush x:Key="MyBrush" po:Freeze="True" Color="Red" />
</Page.Resources>

<!-- Use the frozen brush -->
<Button Background="{StaticResource MyBrush}">Click Me</Button>

</Page>

资料来源: Freezable Objects Overview

关于wpf - 从Freezable派生的WPF对象如何在XAML中卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/799890/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com