Add ResourceDictionary to class library

后端 未结 7 1776
旧巷少年郎
旧巷少年郎 2020-12-14 01:53

I created a class library which is contained of WPF Windows and some user controls inherited from my c# classes that helps me to customize certain wpf controls.

Now

相关标签:
7条回答
  • 2020-12-14 02:22

    To transform a classical library project to a WPF library project (in order to add UserControls, Windows, ResourcesDictionaries, etc.) you can add the following XML in the .csproj file in the first PropertyGroup Node :

    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    

    Full example :

    <?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>
        <!-- ... -->    
    
    0 讨论(0)
  • 2020-12-14 02:25

    @punker76's answer is great and helped me a lot, but it's worth adding that if you create an empty file and add a resource tag into it you should also go to file properties, set BuildAction to Resource, Copy To... to Do not copy and clear CustomTool property if it's set.

    0 讨论(0)
  • 2020-12-14 02:26

    create a resource dictionary like this one

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

    and you can put it where you want

    <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>
    
    0 讨论(0)
  • 2020-12-14 02:26

    In my opinion, the question is about adding a WPF Resource dictionary file to a Class Library project. The answer is that you can't do it for classic Class Library, but for WPF Application project, WPF Custom Control Library project or a WPF User Control Library. For these project types you can add a new Resource Dictionary (WPF), option which is available through adding new item to the project.

    In my opinion the actual question title and question itself does not correspond to the accepted answer.

    0 讨论(0)
  • 2020-12-14 02:35

    Since i can't yet comment but i've used this answer twice now:

    To add to nmariot's answer:


    Tip 1

    to reach the .csproj file from visual studio

    right click project -> click 'unload project'

    right click project [in unloaded state]-> click 'edit 'filename.csproj''

    Tip 2

    to avoid error warnings once resource dictionary has been added:

    add reference to System.Xaml

    0 讨论(0)
  • 2020-12-14 02:36

    Yes. You can add a ResourceDictionary directly to your project.

    When you want to use it, you can merge it into the XAML as needed by using MergedDictionaries to "merge" that standalone ResourceDictionary into the resources of the type (ie: the Window or UserControl).

    0 讨论(0)
提交回复
热议问题