【问题标题】:Loading WPF Style from Resource File从资源文件加载 WPF 样式
【发布时间】:2011-12-07 09:09:41
【问题描述】:

我正在尝试从 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>

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>

来自 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>

我的 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))]

但我仍然无法加载样式。 如果我使用不使用 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>

我做错了什么? 提前致谢

【问题讨论】:

    标签: wpf xaml custom-controls resourcedictionary generic.xaml


    【解决方案1】:

    请为我回答几件事...

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

    如果您添加该引用,资源应该会正确加载。

    我的步骤...

    1. 添加“WPF 自定义控件库”比如“ThemesLibray”
    2. 在“主题”文件夹下添加两个资源字典

    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

      <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. 我有一个主要的启动项目“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>
      

    当我启动 MyWPFTestApp 时,我看到带有绿色文本框的窗口。

    【讨论】:

    • 嗨,也许我写错了,但解决方案包含两个项目,只有 WPF 自定义控件库类型的 WpfTestControls 和 Wpf 应用程序类型的 WpfTestApp,它引用了两个 WpfTestControls。
    • 你是对的,但据我所知,它应该适用于以前的解决方案。这是我现在将采用的解决方法
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 2017-12-07
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多