32

WPFの多くの種類は、から派生していFreezableます。可変POCOオブジェクトに不変性を提供し、特定の状況でパフォーマンスを向上させることができます。

だから私の質問は、XAMLマークアップでオブジェクトをフリーズするにはどうすればよいですか?

(私も同様の、しかし異なる質問を投稿したことに注意してください)。

4

2 に答える 2

45

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>

出典:フリーズ可能なオブジェクトの概要

于 2009-04-29T12:16:04.213 に答える
13

これを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"
于 2009-04-29T11:17:58.003 に答える