将ResourceDictionary添加到类库

dav*_*vor 23 c# wpf class-library resourcedictionary

我创建了一个类库,它包含WPF Windows和一些从我的c#类继承的用户控件,可以帮助我自定义某些wpf控件.

现在我想添加ResourceDictionary,以帮助我在我的wpf类之间共享样式.可能吗?

谢谢.


编辑:位于MY.WpfPresentation.Main项目中的资源字典文件(名为Styles.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
                xmlns:MYNetMisc="clr-namespace:MY.Net.Misc;assembly=MY.Net"
                >
    <Style x:Key="customRowStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}">
        <Setter Property="Foreground" Value="{Binding Path=DataContext.balance, Converter={MYNetMisc:BalanceToColor OnlyNegative=false}}" />
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

使用它:

<MYNetPresentation:frmDockBase.Resources>       
    <ResourceDictionary x:Key="style">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MY.WpfPresentation.Main;component/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <DataTemplate x:Key="TabTemplate">
        <dxlc:LayoutControl Padding="0" ScrollBars="None" Background="Transparent">
            <Image Source="/Images/Icons/table-32x32.png" Width="12" Height="12" />
            <TextBlock Text="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Center" />
        </dxlc:LayoutControl>
    </DataTemplate>

</MYNetPresentation:frmDockBase.Resources>
Run Code Online (Sandbox Code Playgroud)

pun*_*r76 30

创建像这样的资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <!-- Common base theme -->
      <ResourceDictionary Source="pack://application:,,,/Another.AssemblyName;component/YourResDictionaryFolder/OtherStyles.xaml" />
      <ResourceDictionary Source="pack://application:,,,/Another.AssemblyName;component/YourResDictionaryFolder/AnotherStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>

  <!-- store here your styles -->

</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

你可以把它放在你想要的地方

<Window x:Class="DragMoveForms.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2"
        Height="300"
        Width="300">

  <Window.Resources>
    <ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/YourResDictionaryFolder/Dictionary1.xaml" />
  </Window.Resources>

  <Grid>

  </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

  • 好吧,问题是当我尝试向项目添加新项目时没有看到 ResourceDictionary。 (2认同)

Chr*_* W. 13

@ punker76的答案很棒并且对我帮助很大,但值得补充的是,如果你创建一个空文件并在其中添加资源标签,你还应该去文件属性,将BuildAction设置为Resource,Copy To ... to Do not复制并清除CustomTool属性(如果已设置).


nma*_*iot 11

要变换经典库项目的WPF库项目(以添加UserControls,Windows,ResourcesDictionaries等),你可以在第一的PropertyGroup节点上的.csproj文件中添加以下XML:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Run Code Online (Sandbox Code Playgroud)

完整示例:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
    <PropertyGroup>
      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
      <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
      <ProjectGuid>{50E8AAEA-5CED-46BE-AC9A-B7EEF9F5D4C9}</ProjectGuid>
      <OutputType>Library</OutputType>
      <AppDesignerFolder>Properties</AppDesignerFolder>
      <RootNamespace>WpfApplication2</RootNamespace>
      <AssemblyName>WpfApplication2</AssemblyName>
      <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
      <FileAlignment>512</FileAlignment>
      <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
      <WarningLevel>4</WarningLevel>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <TargetFrameworkProfile />
    </PropertyGroup>
    <!-- ... -->    
Run Code Online (Sandbox Code Playgroud)


XMi*_*ght 10

在我看来,问题是将WPF资源字典文件添加到类库项目.答案是您无法为经典类库执行此操作,但对于WPF应用程序项目,WPF自定义控件库项目或WPF用户控件库.对于这些项目类型,您可以添加新的资源字典(WPF)选项,该选项可通过向项目添加新项目来实现.

在我看来,实际的问题标题和问题本身并不符合公认的答案.


Gre*_*rko 5

如果找不到资源字典 (WPF)在尝试创建字典时文件类型,请执行以下操作:

将以下行添加到您的项目文件 (.csproj) 的第一个<PropertyGroup>元素中:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Run Code Online (Sandbox Code Playgroud)

重新加载项目。现在您应该拥有可以在普通 WPF 项目中找到的所有项目类型,包括Resource Dictionary (WPF)