2009-04-28 23 views

Répondre

38

Pour geler un objet Freezable déclaré dans le balisage, vous utilisez l'attribut Freeze défini dans l'espace de noms XML http://schemas.microsoft.com/winfx/2006/xaml/presentation/options.

Dans l'exemple suivant, un SolidColorBrush est déclaré en tant que ressource de page et est gelé. Il est ensuite utilisé pour définir l'arrière-plan d'un bouton.

<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> 

Source: Freezable Objects Overview

+2

Le dernier attribut ne devrait-il pas être 'mc: Ignorable =" po "'? – CodeNaked

+0

@CodeNaked, je crois que vous avez raison. Édité. –

9

Ajouter à votre déclaration d'espace de noms XAML:

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

puis, dans vos objets Freezable, inclure cet attribut

po:Freeze="True" 
+0

Où l'espace de noms 'mc' vient? –

+0

Oh, je crois que j'ai raté ça. Le fixe maintenant. – Botz3000