从资源文件加载 WPF 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7761681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:07:45  来源:igfitidea点击:

Loading WPF Style from Resource File

wpfxamlcustom-controlsresourcedictionarygeneric.xaml

提问by Robob

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

  2. WpfTestApp of type WPF Application Library which has reference to WpfTestControls

  1. WPF 自定义控件库类型的 WpfTestControls

  2. 类型为 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

我究竟做错了什么 ?提前致谢

回答by WPF-it

Please answer few things for me...

请帮我回答几件事...

  1. Is "WPF Custom Control Library" assembly same as "WpfTestControls" assembly?
  2. If not, then does "WPF Custom Control Library" have a reference to the "WpfTestControls" assembly?
  3. Does your WpfTestApphave a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies?
  1. “WPF 自定义控件库”程序集与“WpfTestControls”程序集相同吗?
  2. 如果没有,那么“WPF 自定义控件库”是否引用了“WpfTestControls”程序集?
  3. 您是否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"
  2. In this add two resource dictionaries under "Themes" folder
  1. 添加“WPF 自定义控件库”说“ThemesLibray”
  2. 在这个“Themes”文件夹下添加两个资源字典

TextBoxStyle.xaml

文本框样式.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

通用文件

  <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. In that the window has ThemesLibraryresources merged this way....

    <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>
    
  1. 我有主要的启动项目“MyWPFTestApp”,它具有对 ThemesLibray. 因为该窗口以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 时,我看到带有绿色文本框的窗口。

回答by AnjumSKhan

Main thing is : Make sure you have Build Action of your Resource Dictionary set to Resource.

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