C# 使用WPF自定义控件库(.NET Framework)中普通WPF项目中的App.xaml

C# 使用WPF自定义控件库(.NET Framework)中普通WPF项目中的App.xaml,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个带有App.xaml(不是资源字典)的WPF项目,其中包含一些材料设计材料和一个ViewModelLocator(MVVM),如下所示: <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:

我有一个带有App.xaml(不是资源字典)的WPF项目,其中包含一些材料设计材料和一个ViewModelLocator(MVVM),如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml">
            </ResourceDictionary>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml">
            </ResourceDictionary>
            <!--<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml">
            </ResourceDictionary>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml">
            </ResourceDictionary>-->
            <!-- primary color -->
            <ResourceDictionary>
                <!-- include your primary palette -->
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.teal.xaml">
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
                <!--
                        include three hues from the primary palette (and the associated forecolours).
                        Do not rename, keep in sequence; light to dark.
                    -->
                <SolidColorBrush x:Key="PrimaryHueLightBrush"
                                 Color="{StaticResource Primary100}" />
                <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush"
                                 Color="{StaticResource Primary100Foreground}" />
                <SolidColorBrush x:Key="PrimaryHueMidBrush"
                                 Color="{StaticResource Primary500}" />
                <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush"
                                 Color="{StaticResource Primary500Foreground}" />
                <SolidColorBrush x:Key="PrimaryHueDarkBrush"
                                 Color="{StaticResource Primary700}" />
                <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush"
                                 Color="{StaticResource Primary700Foreground}" />
            </ResourceDictionary>
            <!-- secondary colour -->
            <ResourceDictionary>
                <!-- include your secondary pallette -->
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.teal.xaml">
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
                <!-- include a single secondary accent color (and the associated forecolour) -->
                <SolidColorBrush x:Key="SecondaryAccentBrush"
                                 Color="{StaticResource Accent200}" />
                <SolidColorBrush x:Key="SecondaryAccentForegroundBrush"
                                 Color="{StaticResource Accent200Foreground}" />
            </ResourceDictionary>
            <!-- Include the Dragablz Material Design style -->
            <ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml">
            </ResourceDictionary>

            <ResourceDictionary Source="Resources/CustomMaterialDesignControls.xaml" />

        </ResourceDictionary.MergedDictionaries>
        <!-- tell Dragablz tab control to use the Material Design theme -->
        <Style TargetType="{x:Type dragablz:TabablzControl}"
               BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" />
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True"
                             xmlns:vm="clr-namespace:**WPFProject**.ViewModels" />
    </ResourceDictionary>
</Application.Resources>

当我在WPFProject中创建一个窗口时,它工作得非常好

但我也有一个WPF自定义库项目。如何从自定义WPF项目中的WPFProject访问App.xaml

这是自定义WPF库项目中的Window.xaml:

<Window x:Class="**CustomWPFLibrary**.Views.PersonView"
    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"
    mc:Ignorable="d"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    TextElement.FontWeight="Regular"
    TextElement.FontSize="12"
    TextOptions.TextFormattingMode="Ideal"
    TextOptions.TextRenderingMode="Auto"
    Background="{DynamicResource MaterialDesignPaper}"
    FontFamily="{DynamicResource MaterialDesignFont}"
    Title=" eFenKa - PERSONEN"
    WindowStyle="SingleBorderWindow"
    ResizeMode="CanResize"
    WindowStartupLocation="CenterScreen"
    WindowState="Maximized"
    DataContext="{Binding PersonViewModel, Mode=OneWay, Source={StaticResource Locator}}">
<Grid>

</Grid>


定位器和材料设计内容无法解决。有什么想法吗?或者这可能吗?

TL;博士:我举了一个例子


编辑:另一位GitHub用户发布了一篇关于此主题的文章:


问题 有两个项目,您希望在这两个项目的设计器中显示样式。概括而言:

  • WpfApplication项目(Windows应用程序的输出类型)
  • 自定义控件项目(类库的输出类型)
解决办法 我看到一些帖子建议共享
App.xaml
文件。根据执行方式的不同,可能会创建循环依赖项,或者在尝试强制类库包含ApplicationDefinition时可能会遇到问题。因此,我建议您将样式划分为第三个项目。您的解决方案结构现在如下所示:

  • WpfApplication项目(Windows应用程序的输出类型)
  • 自定义控件项目(类库的输出类型)
  • 样式项目项目(类库的输出类型)
要使这些样式显示在CustomControls项目的设计视图中,并供WPF应用程序使用,请执行以下操作:

  • 和往常一样,在进行更改之前对解决方案进行备份(以防出现问题)
  • 样式项目中创建一个新的ResourceDictionary。这将是一本字典,它合并了所有其他样式和资源,因此可以适当地命名它
  • 将App.xaml中定义的所有与样式相关的资源剪切并粘贴到新的资源字典中
  • WpfApplication项目中,在App.xaml文件中引用新的资源字典。您的应用程序现在看起来应该和您开始进行更改之前一样
  • CustomControls项目中,在
    Properties
    文件夹下创建名为
    DesignTimeResources.xaml
    的新资源字典
  • 构建您的解决方案。注意:您实际上只需要在此时构建CustomControls项目
  • 现在,您必须编辑CustomControlscsproj。注意:您可能必须从VisualStudio卸载项目才能执行此操作
  • CustomControls.csproj
    中,搜索
    DesignTimeResources.xaml
    。您应该找到一个如下所示的块:
  • 
    设计师
    MSBuild:编译
    
  • 将步骤8中的块替换为以下内容:
  • 
    MSBuild:编译
    设计师
    真的
    
  • 现在重新加载/构建CustomControls项目
  • 将对ResourceDictionary的引用从您的样式项目添加到刚才创建的
    DesignTimeResources.xaml
    文件中
  • 重建您的解决方案

  • 现在,您应该可以在CustomControls项目的设计器中看到您的样式

    您不必在应用程序下嵌套资源字典。您可以直接向程序集添加一个,并使用包url引用它。或者,为了使事情更简单,将其添加到窗口的资源中
    @将使我可以将主WPF项目中的App.xaml文件添加到?不,您将添加
    。就这样。您需要资源字典。资源字典可以放在任何具有Resources属性的类中。它是在FrameworkElement类上定义的——请看这里FrameworkElement在WPF中几乎所有类型的类层次结构中。您所需要的只是在需要使用它的地方引用它,或在更高的级别引用它。如果您不了解什么是资源字典或如何使用它们,您可以在这里了解它们。这是较旧的文档,引用了Silverlight,但它仍然适用。@是否喜欢?