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.

  • 相关阅读:
    在Windows中,U盘或者移动硬盘关不掉时,怎么知道是被哪个程序占用了呢?
    选择的文件中包含不支持的格式
    FTO Obesity Variant Circuitry and Adipocyte Browning in Humans
    SNPsnap | 筛选最佳匹配的SNP | 富集分析 | CP loci
    PhastCons | 序列保守性打分
    hg19基因组 | 功能区域 | 位置提取
    投稿SCI杂志 | 如何撰写cover letter | 如何绘制illustrated abstract
    variant的过滤 | filtering and prioritizing genetic variants
    会议录音的处理 | 提高音量 + 降噪 + 自动添加字幕
    小型数据工作站 | 管理和维护 | Jupyter | Rstudio server | Mac & Win10
  • 原文地址:https://www.cnblogs.com/ammar/p/1534747.html
Copyright © 2011-2022 走看看