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.

  • 相关阅读:
    软件工程作业------分析文本文档,统计出现频率最多的十个单词
    关于IBOutlet的生命周期
    重装iTunes 错误代码42401 解决办法
    关于在多个UItextield切换焦点
    关于如何使用代码触发 UIButton的Unwind Segue
    NSFileManager在初始化文件的时候一不留神就进入陷阱
    Xcode的编辑利器Xvim,如何去掉烦人工具栏和文件路径
    __weak 和 __strong 还有Autorelease的用法
    关于NSFetchedResultsController的一些用法
    关于MVC模型的NSNotification用法
  • 原文地址:https://www.cnblogs.com/ammar/p/1534747.html
Copyright © 2011-2022 走看看