Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从资源文件加载WPF样式_Wpf_Xaml_Custom Controls_Resourcedictionary_Generic.xaml - Fatal编程技术网

从资源文件加载WPF样式

从资源文件加载WPF样式,wpf,xaml,custom-controls,resourcedictionary,generic.xaml,Wpf,Xaml,Custom Controls,Resourcedictionary,Generic.xaml,我试图从其他文件加载WPF样式,实际上是从WPF自定义控件库加载的 但我无法加载这里是我的解决方案 该解决方案包含两个项目 WPF自定义控件库类型的WpfTestControls WPF应用程序库类型的WpfTestApp,它引用了WpfTestControls WPF应用程序库中的MainWindow.xaml <Window.Resources> <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">

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

该解决方案包含两个项目

  • WPF自定义控件库类型的WpfTestControls

  • 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>
    
    <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>
    
    <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>
    
    但我仍然无法加载样式。 如果我使用的是Generic.xaml,而不是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自定义控件库”程序集与“WpfTestControls”程序集相同吗
  • 如果没有,那么“WPF自定义控件库”是否有对“WpfTestControls”程序集的引用
  • 您的
    WpfTestApp
    是否同时引用了“WPF自定义控件库”和“WpfTestControls”程序集
  • 如果添加该引用,则应正确加载资源

    我的步骤…

  • 添加一个“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="GreenTextBoxStyle" TargetType="{x:Type TextBox}">
           <Setter Property="Background" Value="Green"/>
        </Style>
     </ResourceDictionary>
    
      <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>
    

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

    主要内容是:确保资源字典的生成操作设置为Resource。

    您好,可能我写错了,但解决方案只包含两个项目WPF自定义控件库类型的WpfTestControls,和Wpf应用程序类型的WpfTestApp,它引用了两个WpfTestControls。您是对的,但据我所知,它应该与以前的解决方案一起工作。这是我目前将采用的解决办法
      <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>
    
    <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>