WPF中的許多類型都來自Freezable
。它爲可變POCO對象提供了不變性,並且在某些情況下可以提高性能。如何從XAML中凍結派生自Freezable的WPF對象?
所以我的問題是,如何凍結XAML標記中的對象?
(請注意,我也發佈了similar but different question)。
WPF中的許多類型都來自Freezable
。它爲可變POCO對象提供了不變性,並且在某些情況下可以提高性能。如何從XAML中凍結派生自Freezable的WPF對象?
所以我的問題是,如何凍結XAML標記中的對象?
(請注意,我也發佈了similar but different question)。
要凍結在標記聲明的Freezable
對象,您使用Freeze
屬性在XML命名空間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>
添加到您的XAML命名空間聲明:
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="po"
然後,在你凍結的對象,包括這個屬性
po:Freeze="True"
在什麼地方 'MC' 命名空間從何而來? – 2009-08-25 09:11:13
哦,我想我錯過了。現在修復它。 – Botz3000 2009-08-25 09:16:13