WPF学习(10)-资源

       资源就是可以让你在程序里面调用的东西,有很多东西,比如需要多次调用,比如一个按钮的样式,每个页面上的按钮希望达成一个统一的风格,那么这个时候就可以封装成资源,然后直接调用这样我们的xaml页面就会显得非常简洁,比如下面这个页面有十个按钮。

    <Grid>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red"></Setter>
                <Setter Property="Foreground" Value="Yellow"></Setter>
                <Style.Triggers >
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="Black"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Button  Content="Button" HorizontalAlignment="Left" Margin="63,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="178,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="178,212,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="63,212,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="318,147,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="318,212,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>

          这样就给多个button使用了同一个样式,效果如下

   

<Window.Resources>        
<!--<TextBlock x:Key="res1" Text="奔骝科技"></TextBlock>        
<TextBlock x:Key="res2" Text="智能无线全解决方案供应商"></TextBlock>-->    </Window.Resources>

定义两个资源,资源放在window窗体范围内,那么在这个范围内,就可以任意使用这个资源。

<Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42"/>
<Button Content="{DynamicResource res2}" HorizontalAlignment="Left" Margin="170,110,0,0" VerticalAlignment="Top" Width="200" Height="42"/>

     直接绑定资源,其中一个使用staticresource,也就是静态资源,另外一个适应dynamicresource,也就是动态资源,效果如下。

       静态资源和动态资源的区别就是,静态资源只读取一次,以后就算更改,也不会改变,而动态资源会随着资源的改变而改变,比如,我们在后台来修改上面两个资源。通过前台页面上的一个按钮,注册一个事件,通过This.Resources[]键去修改他的值,结果就是其中一个会改变,另外一个不会,而改变的就是动态资源,不变的就是静态资源

 this.Resources["res1"] = "室内定位系统";
 this.Resources["res2"] = "无线传感系统";

        当然更多的时候,我们不会在页面中定义资源,而是在外部定义资源,然后到页面中引用,这样的好处是如果需要修改,直接到对应的那块去修改即可。我们先来定义一个外部的资源,直接右击我们的解决方案的项目,右击添加,选择资源字典。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="res1" >123</sys:String>
    <sys:String x:Key="res2" >123</sys:String>
</ResourceDictionary>

      当然,不能忘记的是,要添加命名空间,不然xaml解释器不知道啥是字符串,到任意一个页面绑定资源。

       

    <Window.Resources>
        <ResourceDictionary Source="Dictionary1.xaml">           
        </ResourceDictionary>
    </Window.Resources>

    添加页面对于资源字典的引用,这样就可以在页面中引用了。

 <Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42"/>
 <Button Content="{DynamicResource res2}" HorizontalAlignment="Left" Margin="170,110,0,0" VerticalAlignment="Top" Width="200" Height="42"/>

     效果如下:

      当然,我们可以对于资源字典扩充,比如后期,我们有一个需求,需要修改按钮的背景色为渐变色画刷,那么也很好改,直接在Dictionary1.xaml中修改即可。

      

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="res1" >我是res1对应的资源</sys:String>
    <sys:String x:Key="res2" >我是res2对应的资源</sys:String>
    <LinearGradientBrush x:Key="mybrush">
        <GradientStop Color="Red" Offset="0"/>
        <GradientStop Color="Gray" Offset="1"/>
    </LinearGradientBrush>
</ResourceDictionary>

     

<Button Content="{StaticResource res1}" HorizontalAlignment="Left" Margin="170,29,0,0" VerticalAlignment="Top" Width="200" Height="42" Background="{StaticResource mybrush}"/>

     效果就出来了,非常方便,而且页面的xaml代码也少了很多 ,最重要的是别的页面也可以用,风格就统一啦。

     

猜你喜欢

转载自blog.csdn.net/whjhb/article/details/84583633
今日推荐