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.

  • 相关阅读:
    个人对BFC的见解
    事件简单示例
    visual studio .net 2003
    C# Dictionary 的几种遍历方法
    C# 监听文件夹
    调用SAP dll 出现 试图加载格式不正确的程序
    SAP Connector 类概述
    Sapnco3.0 RFC Server Programs Receive Idocs
    微软工具下载地址
    Sapnco3.0 RFC Client Programs
  • 原文地址:https://www.cnblogs.com/ammar/p/1534747.html
Copyright © 2011-2022 走看看