WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)

简介: 原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
原文: WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)

一、WPF对象级(Window对象)资源的定义与查找

实例一: StaticResource 静态资源(如:皮肤配置方案,运行后不改变)

<Window x:Class="WpfApplication.Window12"
        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"
        Title="Window12" Height="300" Width="300">
    <!--将System命名空间引入到xaml代码并映射为sys命名空间-->
    <!--在Window.Resources属性添加两个资源条目-->
    <Window.Resources>
       <!--键值对资源-->
        <ResourceDictionary>
            <sys:String x:Key="str">
                字符
            </sys:String>
            <sys:Double x:Key="dbl">
                3.1415926
            </sys:Double>
        </ResourceDictionary>       
    </Window.Resources>
    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource ResourceKey=str}" Margin="5"/>
    </StackPanel>
</Window>

简化:

<Window x:Class="WpfApplication.Window12"
        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"
        Title="Window12" Height="300" Width="300">
    <!--将System命名空间引入到xaml代码并映射为sys命名空间-->
    <!--在Window.Resources属性添加两个资源条目-->
    <Window.Resources>

    </Window.Resources>

    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource str}" Margin="5"/>
    </StackPanel>
</Window>


在C#代码中,使用定义的XAML代码中定义的资源。

            //string text = (string)FindResource("str");
            string text = (string)this.Resources["str"];//键值获取
            this.textblock1.Text = text;

实例二:把资源像CSS或JavaScript放到独立文件中。


新建 “资源字典”


Themes 文件夹中的 ShinyRed.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="str">字符</sys:String>
    <sys:Double x:Key="dbl">3.1415926</sys:Double>
</ResourceDictionary>

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window12" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary Source="Themes/ShinyRed.xaml"/>
    </Window.Resources>
    <StackPanel>
        <TextBlock Name="textblock1"  Margin="5" Text="{StaticResource str}"/>
    </StackPanel>
</Window>

合并多个外部资源字典成为本地字典

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Xml/TreeFile.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

实例三、DynamicResource 动态资源(如:运行时,允许用户更改皮肤)

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window12" Height="300" Width="300">
    <Window.Resources>
        <TextBlock x:Key="res1" Text="动态资源"/>
    </Window.Resources>
    <StackPanel>
        <Button Content="{DynamicResource res1}"/>
        <Button Content="Update" Click="Button_Click"/>
    </StackPanel>
</Window>

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["res1"] = new TextBlock { Text="换皮肤"};
        }

三、向程序添加二进制资源

1、Resources.resx 文件添加 字符串二进制资源

双击 Resources.resx 文件


代码:

<Window x:Class="WpfApplication.Window13"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prop="clr-namespace:WpfApplication.Properties"
        Title="Window13" Height="300" Width="300">
    <Grid>
        <TextBox Text="{x:Static prop:Resources.UserName}"/>
    </Grid>
</Window>


2、Resources.resx 文件添加 图片二进制资源

新建文件夹


设置图片属性


代码:

<Image Name="img" Source="Resources/Images/4.jpg" Stretch="Fill"/>

或者

img.Source = new BitmapImage(new Uri(@"Resources/Images/4.jpg", UriKind.Relative));


四、资源绑定树

效果:


TreeFile.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib"
                    >
    <XmlDataProvider x:Key="xdp" XPath="FileSystem/Folder">
        <x:XData>
            <FileSystem xmlns="">
                <Folder Name="基础设置">
                    <Folder Name="数据库地址" Value="Pages/Page_SqlAddress.xaml"/>
                    <Folder Name="配置一次图" Value="Pages/Page_ConfigControls.xaml"/>
                </Folder>
                <Folder Name="终端模板管理">
                    <Folder Name="终端管理" Value="Pages/Page_Products.xaml"/>
                    <Folder Name="终端变量管理" Value="Pages/Page_Varibles.xaml"/>
                </Folder>
                <Folder Name="配置生成">
                    <Folder Name="生成全部" Value="Pages/Page_BuildAll.xaml"/>
                    <Folder Name="单步生成-终端" Value="Pages/Page_BuildProducts.xaml"/>
                    <Folder Name="单步生成-终端变量" Value="Pages/Page_BuildVaribles.xaml"/>
                    <Folder Name="单步生成-设备" Value="Pages/Page_BuildEquipment.xaml"/>
                    <Folder Name="单步生成-一次图" Value="Pages/Page_BuildGraphical.xaml"/>
                </Folder>
            </FileSystem>
        </x:XData>
    </XmlDataProvider>
<ResourceDictionary>


App.xaml

<Application x:Class="AutomaticConfigurationAPP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             >
    <!--启始页等一堆参数设置,相当于控制台程序Main,是整个程序入口-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Xml/TreeFile.xaml" />
                <ResourceDictionary Source="pack://application:,,,/AutomaticConfigurationAPP;component/Styles/Bootstrap.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/AutomaticConfigurationAPP;component/PathGeometries/Glyphicons.xaml"/>
                <!--<ResourceDictionary Source="/Themes/Theme.xaml" />-->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>


ViewModel层

<Window x:Class="AutomaticConfigurationAPP.MWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="配置文件自动生成工具" Height="500" Width="800" Icon="/AutomaticConfigurationAPP;component/bitbug_favicon%20%282%29.ico">
    <Window.Background>
        <ImageBrush ImageSource="/AutomaticConfigurationAPP;component/Images/background_blue.jpg" Stretch="Fill" TileMode="Tile" Viewport="0,0,1174,600" ViewportUnits="Absolute" />
    </Window.Background>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="2"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="8"/>
        </Grid.RowDefinitions>
        <TreeView  ItemsSource="{Binding Source={StaticResource xdp}}" Grid.Row="0" Grid.Column="0" x:Name="TreeRoot" Background="#E5EBF3">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">
                        <TextBlock x:Name="a" Text="{Binding XPath=@Name}" FontSize="12"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <Frame x:Name="PageContext" Grid.Row="0" Grid.Column="2"  Background="#F3F3F3" BorderBrush="#888" BorderThickness="1" NavigationUIVisibility="Visible"
               Source="{Binding ElementName=TreeRoot, Path=SelectedItem.Attributes[Value].Value}"/>
    </Grid>
</Window>







目录
相关文章
WPF—多重绑定和跨层级绑定
WPF—多重绑定和跨层级绑定
|
2月前
|
C#
浅谈WPF之样式与资源
WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。
42 0
|
9月前
|
移动开发 开发框架 网络协议
WPF+ASP.NET SignalR实现动态折线图
WPF+ASP.NET SignalR实现动态折线图
81 0
|
9月前
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
116 0
|
12月前
|
C#
WPF更新绑定字段
WPF更新绑定字段
69 0
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
|
前端开发 C#
WPF 之 数据与命令绑定 (MVVM方式)
WPF 之 数据与命令绑定 (MVVM方式)
160 0
WPF 之 数据与命令绑定 (MVVM方式)
|
C#
WPF中的一个bug造成CPU资源拉满
WPF中的一个bug造成CPU资源拉满
169 0
|
IDE 编译器 C#
WPF实现强大的动态公式计算
数据库可以定义表不同列之间的计算公式,进行自动公式计算,但如何实现行上的动态公式计算呢?行由于可以动态扩展,在某些应用场景下将能很好的解决实际问题。本文就探讨一下如何在WPF中实现一种基于行字段的动态公式计算。
990 0
WPF实现强大的动态公式计算
|
C#
WPF资源字典怎么用
资源字典的使用方法
186 0
WPF资源字典怎么用