创建包含ResourceDictionary的DLL

时间:2017-01-24 10:38:30

标签: c# wpf xaml

我创建了一个新的WPF项目。创建项目后,我将项目属性更改为 outputtype = classlibrary

这是我的文件结构:

enter image description here

CustomWindow.xaml是一个ResourceDictionary,用于描述CustomWindow.cs的外观。 文件CustomWindow.xaml看起来像

<ResourceDictionary x:Class="WpfApplication1.CustomWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">

  <Style x:Key="MainWindow" TargetType="{x:Type Window}">
     <Setter Property="Template">
         <!-- ... -->  
     </Setter>
  </Style>
</ResourceDictionary>

班级CustomWindow.xaml看起来像

namespace WpfApplication1
{
    public partial class CustomWindow : ResourceDictionary
    {
        public CustomWindow()
        {
            InitializeComponent();
        }
    }
}

现在我已添加到App.xaml以下

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Common base theme -->
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/CustomWindow.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我将文件CustomWindow.xaml属性构建过程更改为Page

之后我构建了lib。现在我想将样式MainWindow用于我的新项目的主窗口。我已将lib添加到项目引用中。并且App.xaml我添加了一些代码:

<Application x:Class="WpfApplication2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Common base theme -->
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/CustomWindow.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我试图建立它,但我变成了两个错误。 第一个

  

无法找到资源“MainWindow”。

第二个

  

查找资源字典“pack:// application:,,, / WpfApplication1; component / CustomWindow.xaml”时发生错误。

也许有人有个主意。

1 个答案:

答案 0 :(得分:2)

  1. 在Visual Studio中创建一个新的 WPF用户控件库项目。
  2. 将新的ResourceDictionary添加到您定义窗口样式的项目中。
  3. WPF应用程序项目添加对 WPF用户控件库项目的引用。
  4. 修改WPF应用程序项目中的App.xaml文件:

    <Application x:Class="WpfApplication2.App"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:local="clr-namespace:WpfApplication1"
                     StartupUri="Window9.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <!-- Common base theme -->
                    <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/CustomWindow.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    
  5. &#34; WpfControlLibrary1&#34;是上面示例标记中的WPF用户控件库的名称和&#34; CustomWindow&#34;是此项目中资源字典的名称。

    1. 在WPF应用程序中设置MainWindow的Style属性,如下所示:

      <Window x:Class="WpfApplication2.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:WpfApplication2"
              mc:Ignorable="d"
              Title="MainWindow" Height="350" Width="525" Style="{StaticResource MainWindow}">
          <Grid>
      
          </Grid>
      </Window>