zoukankan      html  css  js  c++  java
  • WPF使用资源字典组织资源

    首先在解决方案资源管理器中添加一个或多个资源词典(资源字典),并向多个资源字典中添加对象资源信息。示例中新建了三个资源字典,并向资源字典中添加了对象资源,代码如下。

    第一个资源字典:(第一个资源字典命名为MyDictionary1.xaml)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ImageBrush x:Key="MyBrush1"
    TileMode="Tile"
    ViewportUnits="Absolute"
    Viewport="0 0 30 30"
    ImageSource="/image/1.png"
    Opacity="0.9"/>
    </ResourceDictionary>

    第二个资源字典:(第二个资源字典命名为MyDictionary2.xaml)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <LinearGradientBrush x:Key="MyBrush2">
    <LinearGradientBrush.GradientStops>
    <GradientStopCollection>
    <GradientStop Color="Red" Offset="0.1"/>
    <GradientStop Color="Green" Offset="0.5"/>
    <GradientStop Color="Blue" Offset="0.9"/>
    </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
    </ResourceDictionary>

    第三个资源字典:(第三个资源字典命名为MyDictionary3.xaml)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="MyBrush3" Color="Blue"/>
    </ResourceDictionary>

    在创建了多个资源字典后可以将它们合并到主窗体的资源集合中,示例代码如下。

    <Window.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="MyDictionary1.xaml"/>
    <ResourceDictionary Source="MyDictionary2.xaml"/>
    <ResourceDictionary Source="Mydictionary3.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <ImageBrush x:Key="MyBrush"
    TileMode="Tile"
    ViewportUnits="Absolute"
    Viewport="0 0 50 50"
    ImageSource="/image/1.png"
    Opacity="0.9"/>
    </ResourceDictionary>
    </Window.Resources>

    将多个资源字典和并到窗体资源集合后,就可以像使用窗体中定义的资源一下使用多个资源字典中的资源了,示例代码如下。

    <Window x:Class="WPF中的对象集合.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="MyDictionary1.xaml"/>
    <ResourceDictionary Source="MyDictionary2.xaml"/>
    <ResourceDictionary Source="Mydictionary3.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <ImageBrush x:Key="MyBrush"
    TileMode="Tile"
    ViewportUnits="Absolute"
    Viewport="0 0 50 50"
    ImageSource="/image/1.png"
    Opacity="0.9"/>
    </ResourceDictionary>
    </Window.Resources>
    <Grid>
    <Button Background="{StaticResource MyBrush1}"></Button>
    </Grid>
    </Window>


     

  • 相关阅读:
    【SPOJ1825】Free Tour II-点分治+桶排序
    【BZOJ3238】差异(AHOI2013)-后缀自动机+树形DP
    Vacation
    Function
    Path
    杭电oj初体验之 Code
    挑7
    行游散记!
    坐标移动
    STL之pair类型
  • 原文地址:https://www.cnblogs.com/zhangtao/p/1998295.html
Copyright © 2011-2022 走看看