简体   繁体   English

从资源文件加载WPF样式

[英]Loading WPF Style from Resource File

I am trying to load WPF Style from other file actually from WPF Custom Control Library but i am failing to load here is my solution. 我试图从其他文件加载WPF样式实际上从WPF自定义控件库,但我无法加载这里是我的解决方案。

The solution contains two projects 该解决方案包含两个项目

  1. WpfTestControls of Type WPF Custom Control Library WPF自定义控件库类型的WpfTestControls

  2. WpfTestApp of type WPF Application Library which has reference to WpfTestControls WPF应用程序库类型的WpfTestApp,它引用了WpfTestControls

MainWindow.xaml from WPF Application Library 来自WPF应用程序库的MainWindow.xaml

<Window.Resources>
    <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBox Height="50px" Width="100px" Style="{DynamicResource TempStyle}"/>
</Grid>

Generic.xaml from WPF Custom Control Library 来自WPF自定义控件库的Generic.xaml

<ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/WpfTestControls;component/TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

TextBoxStyle.xaml from WPF Custom Control Library 来自WPF自定义控件库的TextBoxStyle.xaml

<ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Green"/>
</Style>

My AssemblyInfo.cs file contains the following 我的AssemblyInfo.cs文件包含以下内容

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

But still i am failing to load the Style. 但我仍然没有加载风格。 If i am using the not using the Generic.xaml everything work fine for example the following code works as expected 如果我使用不使用Generic.xaml一切正常,例如下面的代码按预期工作

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBox Height="50px" Width="100px" Style="{StaticResource TempStyle}"/>
</Grid>

What am i doing wrong ? 我究竟做错了什么 ? Thanks in advance 提前致谢

Please answer few things for me... 请为我回答几件事......

  1. Is "WPF Custom Control Library" assembly same as "WpfTestControls" assembly? “WPF自定义控件库”程序集与“WpfTestControls”程序集相同吗?
  2. If not, then does "WPF Custom Control Library" have a reference to the "WpfTestControls" assembly? 如果没有,那么“WPF自定义控件库”是否引用了“WpfTestControls”程序集?
  3. Does your WpfTestApp have a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies? 您的WpfTestApp是否同时引用“WPF自定义控件库”和“WpfTestControls”程序集?

If you add that reference(s), the resources should load correctly. 如果添加该引用,则应正确加载资源。

My Steps... 我的步骤......

  1. Add a "WPF Custom Control Library" say "ThemesLibray" 添加“WPF自定义控件库”说“ThemesLibray”
  2. In this add two resource dictionaries under "Themes" folder 在此添加“Themes”文件夹下的两个资源字典

TextBoxStyle.xaml TextBoxStyle.xaml

 <ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="GreenTextBoxStyle" TargetType="{x:Type TextBox}">
       <Setter Property="Background" Value="Green"/>
    </Style>
 </ResourceDictionary>

Generic.xaml Generic.xaml

  <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="TextBoxStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
  1. I have main starup project "MyWPFTestApp" that has assembly reference to ThemesLibray . 我有主要的starup项目“MyWPFTestApp”,它有对ThemesLibray程序集引用。 In that the window has ThemesLibrary resources merged this way.... 在那个窗口中ThemesLibrary这种方式合并了ThemesLibrary资源....

     <Window x:Class="MyWPFTestApp.Window7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window7" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/ThemseLibrary;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <TextBox Style="{StaticResource GreenTextBoxStyle}"/> </Grid> </Window> 

When I launch MyWPFTestApp, I see the Window with green TextBox. 当我启动MyWPFTestApp时,我看到带有绿色TextBox的Window。

主要是:确保将资源字典的构建操作设置为资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM