[原]wpf foundation Part 1 of n (object resource )

 

Wpf foundation part 1 of n (Wpf object   Resources )

What is resources ?

Windows Presentation Foundation (WPF) resources provide a simple way to reuse commonly defined objects and values.

Introduction :

Wpf support tow kinds of resources Assembly resources and object resources  , in this article we just explian the object resources  , the object resources are similar to methods or functions we can define method and reuse it any where in application ( far of access control of methods) , wpf object resources can also define with window( main interface) , with control (like button , border , rectangle .. ect).

 

Where to define resources ?

 

 Every element includes a Resources property, which stores a dictionary collection of resources.

(It’s an instance of the ResourceDictionary class.) The resources collection can hold any type of

object, indexed by string.

Although every element includes the Resources property (which is defined as part of the

FrameworkElement class), the most common way to define resources is at the window-level.

That’s because every element has access to the resources in its own resource collection and

the resources in all of its parents’ resource collections.

 


<Window.Resources>
 
<!-- here we define gradientBrush( think as type) we give it name CrearoBrush  this static resource juts mix the White and red color as brush you can resue it latter. -->
 
<LinearGradientBrush x:Key="WindowLevelCrearoBrush" EndPoint="0.5,0" StartPoint="0.5,1">
            
<GradientStop Color="Red" Offset="0"/>
            
<GradientStop Color="White" Offset=" 1"/>
        
</LinearGradientBrush>
  
</Window.Resources>
    
<!-- to call the CrearoBrush-->
    
<Grid Background ="{StaticResource WindowLevelCrearoBrush}"></Grid>
</Window

 


This Brush you also can call it during Runtime using c#,VB.

 Like below

 

Now the time for resuing those static predefined resources

 

private void button1_Click(object sender, RoutedEventArgs e)
 {
//
  this.Background =(Brush)this.FindResource("WindowLevelCrearoBrush");
 }

 

 

Here also you can define resource within contols

 


<Button Height="54" Margin="111,27,24,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">Button
            
<Button.Resources >
                
<ImageBrush x:Key="ControlLevelCrearoImageBrush" ImageSource="crearo.png">
                
</ImageBrush>
            
</Button.Resources>
        
</Button

 

 

Here the way to call it from c# code


private void button1_Click(object sender, RoutedEventArgs e)
        {
            
//becoze it defined in button brush button1.FindResource
            this.Background = (ImageBrush)button1.FindResource("ControlLevelCrearoImageBrush");
}

 

bject resources have a number of important benefits ( pro c# 2008)


Efficiency. Resources let you define an object once and use it in several places in your

markup. This streamlines your code and makes it marginally more efficient.

Maintainability. Resources let you take low-level formatting details (such as font sizes)

and move them to a central place where they’re easy to change. It’s the XAML equivalent

of creating constants in your code.

Adaptability. Once certain information is separated from the rest of your application

and placed in a resource section, it becomes possible to modify it dynamically. For

example, you may want to change resource details based on user preferences or the

current language.

 

 

 

 

posted @ 2009-07-30 10:07  ammar  阅读(455)  评论(3编辑  收藏  举报