一、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放到独立文件中。
新建 “资源字典”