WPFの多くの種類は、から派生していFreezable
ます。可変POCOオブジェクトに不変性を提供し、特定の状況でパフォーマンスを向上させることができます。
だから私の質問は、XAMLマークアップでオブジェクトをフリーズするにはどうすればよいですか?
(私も同様の、しかし異なる質問を投稿したことに注意してください)。
WPFの多くの種類は、から派生していFreezable
ます。可変POCOオブジェクトに不変性を提供し、特定の状況でパフォーマンスを向上させることができます。
だから私の質問は、XAMLマークアップでオブジェクトをフリーズするにはどうすればよいですか?
(私も同様の、しかし異なる質問を投稿したことに注意してください)。
Freezable
マークアップで宣言されたオブジェクトをフリーズするFreeze
には、XML名前空間で定義された属性を使用しますhttp://schemas.microsoft.com/winfx/2006/xaml/presentation/options
。
次の例では、aSolidColorBrush
がページリソースとして宣言され、フリーズされています。次に、ボタンの背景を設定するために使用されます。
<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"