zoukankan      html  css  js  c++  java
  • [原]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.

  • 相关阅读:
    26个新鲜有魅力的自适应网站设计实例
    计划——Redis
    Go语言工具go get的一点问题
    JNDI数据源的配置及使用 (2010-11-21 21:16:43)转载▼
    策略模式
    在Sping的配置文件中,关于dataSource的配置,就我们常用的方法大致可以有三种:
    Spring 中 用 ${xxx} 读取properties文件的说明
    Spring中属性文件properties的读取与使用
    通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
    设置xml以让通知spring 扫描 注解
  • 原文地址:https://www.cnblogs.com/ammar/p/1534747.html
Copyright © 2011-2022 走看看