从资源文件加载WPF样式

Rob*_*bob 5 wpf xaml custom-controls resourcedictionary generic.xaml

我试图从其他文件加载WPF样式实际上从WPF自定义控件库,但我无法加载这里是我的解决方案.

该解决方案包含两个项目

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

  2. WPF应用程序库类型的WpfTestApp,它引用了WpfTestControls

来自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>
Run Code Online (Sandbox Code Playgroud)

来自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>
Run Code Online (Sandbox Code Playgroud)

来自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>
Run Code Online (Sandbox Code Playgroud)

我的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))]
Run Code Online (Sandbox Code Playgroud)

但我仍然没有加载风格.如果我使用不使用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>
Run Code Online (Sandbox Code Playgroud)

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

WPF*_*-it 6

请为我回答几件事......

  1. "WPF自定义控件库"程序集与"WpfTestControls"程序集相同吗?
  2. 如果没有,那么"WPF自定义控件库"是否引用了"WpfTestControls"程序集?
  3. 您是否WpfTestApp同时参考"WPF自定义控件库"和"WpfTestControls"程序集?

如果添加该引用,则应正确加载资源.

我的步骤......

  1. 添加"WPF自定义控件库"说"ThemesLibray"
  2. 在此添加"Themes"文件夹下的两个资源字典

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)
  1. 我有主要的starup项目"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>
    
    Run Code Online (Sandbox Code Playgroud)

当我启动MyWPFTestApp时,我看到带有绿色TextBox的Window.