WPF中的许多类型都是从Freezable派生的。它为可变的POCO对象提供了不变性,并允许在某些情况下提高性能。
所以我的问题是,如何冻结XAML标记中的对象?
(请注意,我也发布了一个similar but different question )。
发布于 2009-04-29 12:16:04
要冻结在标记中声明的Freezable对象,可以使用在XML名称空间http://schemas.microsoft.com/winfx/2006/xaml/presentation/options中定义的Freeze属性。
在下面的示例中,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
发布于 2009-04-29 11:17:58
将此代码添加到xaml名称空间声明中:
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="po"然后,在freezable对象中包含以下属性
po:Freeze="True"https://stackoverflow.com/questions/799890
复制相似问题