WPF中资源字典(ResourceDictionary)的使用
资源字典(ResourceDictionary)的使用
1、建立资源文件
各资源文件中,若需要引入项目中其它文件,则可通过命名空间引入。对应各资源元素,设置x:key值。代码中通过x:key值获取相应的资源。以参数值转换器为例。
ValueConverter.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:valueConvert="clr-namespace:AHUSelectorPlatformUI.ValueConvert">
<!--界面显示相关-->
<valueConvert:ConvertWindMachineTypeToTreeViewSource x:Key="ConvertToTreeViewSource"/>
<valueConvert:UnitPowerConvert x:Key="UnitPowerConvert"/>
<!--........-->
</ResourceDictionary>
2、项目中引入资源
有以下两种方法
1、WPF项目中的app.xaml文件中,可以通过ResourceDictionary标签,引入各类资源。如下代码,其中引入了自定义样式、数据模板、窗体界面模板、数值转换器等资源。不同的资源位于不同的文件地址,可直接通过Source属性链接资源路径。
<Application x:Class="AHUSelectorPlatform.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"
StartupUri="View\Login.xaml"
mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Style/SimpleStyles.xaml" />
<ResourceDictionary Source="/Resources/SettingResource/ValueConverter.xaml" />
<ResourceDictionary Source="/View/Segments/DataTemplate/SegmentDataTemplate.xaml" />
<ResourceDictionary Source="/Resources/SettingResource/WindowResource.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
2、通过newResourceDictionary的方式建立资源并绑定。
public static void loadRd()
{
var brushes_rd = new ResourceDictionary()
{
Source = new Uri("pack://Application:,,,/testXml;component/dictionary.xaml")
};
Application.Current.Resources = new ResourceDictionary()
{
MergedDictionaries =
{
brushes_rd
}
};
}
3、使用资源
引入资源后,项目中则可通过各类的方法,获取到相应的资源。
-
界面参数绑定时,可以使用参数值转换器进行参数转换。如下拉值转换成文本。数值转换成对象等。如下代码,BrandConvert为ValueConverter.xaml中的资源。
-
设置静态资源来源。如下代码,UIDataContainer为SimpleStyles.xaml中的资源。
<ComboBox Name="FanBrand" SelectedIndex="{Binding FanBrand,Converter={StaticResource BrandConvert},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <ComboBoxItem Content="{Binding StandardFanBrand, Mode=OneWay, Source={StaticResource UIDataContainer}, UpdateSourceTrigger=PropertyChanged}"/> </ComboBox>
-
设置数据绑定模板。如下代码,PressureDataTemplate为WindowResource.xaml中的资源。
<UserControl x:Class="AHUSelectorPlatformUI.View.Segments.DLD.BlowerFanSeg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Height="auto" Width="300" Background="White"> <ContentControl x:Name="ContentControl" ContentTemplate="{DynamicResource PressureDataTemplate}" Content="{Binding}" /> </UserControl>
-
使用Application.Current.TryFindResource()方法获取对应资源。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Application.Current.TryFindResource(ChooseSegCodeName.WindowReflectDictionary[(SegmentID)value]) as UserControl; }
将资源全局注册
若定义的某个资源,需要给应用全局使用,可以在 App.xaml 中完成注册:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/应用程序名;component/资源的具体路径" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
具体实现如下:
<Application x:Class="MVVMLightDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="View/BindingFormView.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MVVMLightDemo;component/Assets/TextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>