WPF 4: Cached Composition

Un'altra delle novità introdotte da Windows Presentation Foundation 4 si chiama cached composition e, in parole molto povere, permette di memorizzare un elemento visuale all'interno di una cache in modo da recuperarlo rapidamente senza doverlo renderizzare graficamente ogni qual volta lo si utilizza. Si definisce un oggetto BitmapCache all'interno delle risorse del contenitore che andrà memorizzato in cache, come nel caso seguente in cui è presente un TextBlock:

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition />

        </Grid.RowDefinitions>

        <!-- Stabilisce come deve andare in cache il contenuto

             della Grid -->

        <Grid.CacheMode>

            <!-- In questo caso si usa la cache quando si fa lo

                 zoom al doppio delle dimensioni -->

            <BitmapCache RenderAtScale="2" EnableClearType="False"/>

        </Grid.CacheMode>

       

        <!--Il contenuto poi si dichiara normalmente-->

        <TextBlock Text="Cached in case you zoom this"

                   FontSize="20" Grid.Row="0" />

 

Da notare come il contenitore esponga ora una proprietà CacheMode all'interno della quale si definisce la modalità di applicazione della tecnica. Si utilizza la proprietà RenderAtScale per determinare a quale livello di zoom l'opzione di cache debba essere effettivamente applicata. Si disabilitano i caratteri clear type per migliorare le performance. Nell'esempio sopra esposto, il caching avviene qualora il TextBlock subisca una ScaleTransform che ne raddoppi le dimensioni. Un altro esempio è proprio quello in cui sappiamo a priori che un elemento dell'interfaccia verrà utilizzato più volte e quindi può essere conveniente memorizzarlo in cache. L'esempio seguente mostra come applicare un'immagine allo sfondo di due TextBlock utilizzando la cached composition:

 

        <StackPanel Grid.Row="1">

            <StackPanel.Resources>

                <Image x:Key="cachedImage" Source="LogoVBTT.gif">

                    <!-- Poichè so che utilizzerò la stessa image

                         più volte, la metto in cache così non

                         verrà ripetutamente renderizzata -->

                    <Image.CacheMode>

                        <!-- stavolta il caching avviene alle dimensioni

                             naturali -->

                        <BitmapCache RenderAtScale="1" EnableClearType="False"

                                     SnapsToDevicePixels="False"/>

                    </Image.CacheMode>

                </Image>

                <!-- Utilizzo un pennello BitmapCacheBrush

                     come riempimento -->

                <BitmapCacheBrush x:Key="cachedBrush"

                    Target="{StaticResource cachedImage}"/>

            </StackPanel.Resources>

           

            <TextBlock Text="Text one..." FontSize="24" Height="60" Foreground="Red"

                       FontWeight="Bold"

                       Background="{StaticResource cachedBrush}"/>

            <TextBlock Text="Text two..." FontSize="24" Height="60" Foreground="Red"

                       FontWeight="Bold"

                       Background="{StaticResource cachedBrush}"/>

        </StackPanel>

    </Grid>

In maniera differente dal primo esempio, qui la tecnica si applica al singolo elemento (proprietà Image.CacheMode). Per utilizzare un elemento in cache si utilizza un oggetto chiamato BitmapCacheBrush che può essere utilizzato come riempimento di altri elementi, come dimostrato dai due TextBlock. Il risultato, puramente dimostrativo, del codice è il seguente:

Alessandro

posted @ Thursday, December 03, 2009 3:35 PM

Print

Comments on this entry:

# re: rolex

Left by ass@sina.com at 1/25/2010 2:16 AM
Gravatar
It patek philippe would never be enough tag heuer for the market to have breitling watches watches in new styles. No matter how many incarnations of style and design we see rolex every season in leather watches for men, this truly timeless fashion staple is bound for the audemars piguet wrists of men around the globe for the foreseeable future! Leather is really omega watches good material. It makes many products from it elegant and outstanding.Recent trends montblanc watches in leather watches for men lead us to the cuff-like timepiece. Fashioned cartier watches after stylish leather man-bracelets, these leather watches for men panerai watches are masculine in attitude and bold in design. Cuff inspired leather watches bvlgari watches for men are not for the faint of heart or style. Ed Hardy's leather watches for men armani watches are hot items when it comes to the leather bracelet look!

# re: rolex

Left by rolex at 1/25/2010 2:18 AM
Gravatar
replica watches For a less flamboyant look in leather watches for men, omega watches try out the tag heuer Guess 'Black Leather Strap Watch,' which boasts a modern exposed movement in a square face, breitling watches placed atop a tapered leather band in matte black. Either of these styles of cuffed leather watches for men cartier watches would look fabulous with a fake watches striped button-down shirt and stone washed jeans. Who said animal print was a female style? Leather watches for men are stunningly handsome when replica rolex rembossed with crocodile or even snake print. Omega embodies this tasteful trend in their 'Aqua Terra Railmaster' line of leather watches for men tag heuer . A replica watches beautiful black analog face in stainless steel is replica watches complemented by rich brown crocodile leather, and kept together with classic white stitching tag heuer.

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 3 and type the answer here:
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234