使用WPF自定义控件库(.NET Framework)中普通WPF项目中的App.xaml [英] Use the App.xaml from a normal WPF project in a WPF Custom Control Library (.NET Framework)

查看:125
本文介绍了使用WPF自定义控件库(.NET Framework)中普通WPF项目中的App.xaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WPF project with App.xaml (not a resource dictionary) with some material design stuff and a ViewModelLocator(MVVM) that looks like this:

<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中创建一个窗口时,它可以完美地工作.

It works perfect when I create a Window in WPFProject.

但是我也有一个WPF Custome Library项目.如何在我的自定义WPF项目中从WPFProject访问App.xaml.

But I also have a WPF Custome Library project. How can I acces the App.xaml from WPFProject in my Custom WPF project.

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

This is a Window.xaml in Custom WPF Library project:

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

定位器和MaterialDesign内容无法解析.有任何想法吗?还是有可能吗?

Locator and the MaterialDesign stuff can't be resolved. Any ideas? Or is this even possible?

推荐答案

TL; DR:我在 GitHub

TL;DR: I put an example on GitHub

:另一个GitHub用户在此主题上发布了不错的文章:

a different GitHub user posted a decent article on this topic: article.

有两个项目,您希望在两个设计器中显示样式.概括一下:

There are two projects and you would like styling to be shown in the designer of both. To generalize:

  • 一个 WpfApplication 项目(WindowsApplication的输出类型)
  • 一个 CustomControls 项目(ClassLibrary的输出类型)
  • a WpfApplication project (output type of WindowsApplication)
  • a CustomControls project (output type of ClassLibrary)

我看到过一些帖子,提示要共享App.xaml文件.根据完成操作的方式,可能会创建循环依赖关系,或者在尝试强制类库包含ApplicationDefinition时会遇到问题.因此,我建议您将样式分离到第三个项目中.您的解决方案结构现在将如下所示:

I have seen posts suggesting that the App.xaml file be shared. Depending on how that is done, a circular dependency could be created or you could run into problems with trying to force a class library to contain an ApplicationDefinition. So instead, I suggest that you separate out your styling into a third project. Your solution structure will now look like:

  • 一个 WpfApplication 项目(WindowsApplication的输出类型)
  • 一个 CustomControls 项目(ClassLibrary的输出类型)
  • 一个 StylesProject 项目(ClassLibrary的输出类型)
  • a WpfApplication project (output type of WindowsApplication)
  • a CustomControls project (output type of ClassLibrary)
  • a StylesProject project (output type of ClassLibrary)

要使这些样式出现在 CustomControls 项目的设计视图中,并由您的 WpfApplication 使用:

To have these styles appear in the design view of the CustomControls project and to be used by your WpfApplication:

  1. 与往常一样,在进行更改之前备份解决方案(以防万一出现问题).
  2. 在您的 StylesProject 中,创建一个新的ResourceDictionary.这是将合并您所有其他样式和资源的词典,因此请适当命名.
  3. 将您在App.xaml中定义的所有与样式相关的资源剪切并粘贴到新的资源字典中.
  4. 在您的 WpfApplication 项目中,在App.xaml文件中引用新的资源字典.您的应用程序现在应该看起来像在开始进行更改之前一样.
  5. 在您的 CustomControls 项目中,在Properties文件夹下创建一个名为DesignTimeResources.xaml的新ResourceDictionary.
  6. 构建您的解决方案. 注意:您实际上只需要在此时构建 CustomControls 项目.
  7. 您现在必须编辑 CustomControls csproj. 注意:您可能必须卸载项目才能从Visual Studio中进行操作.
  8. CustomControls.csproj中,搜索DesignTimeResources.xaml.您应该找到一个类似于以下内容的块:
  1. As always, make a backup of your solution before making changes (just in case something goes wrong).
  2. In your StylesProject, create a new ResourceDictionary. This will be the dictionary that merges all of your other styles and resources so name it appropriately.
  3. Cut and paste all of your style related resources that you had defined in your App.xaml into your new resource dictionary.
  4. In your WpfApplication project, reference your new resource dictionary in your App.xaml file. Your application should now look like it did before you started making changes.
  5. In your CustomControls project, create a new ResourceDictionary named DesignTimeResources.xaml under the Properties folder.
  6. Build your solution. Note: you really only have to build the CustomControls project at this point in time.
  7. You now have to edit the CustomControls csproj. Note: you may have to unload the project to do it from Visual Studio.
  8. In CustomControls.csproj, search for DesignTimeResources.xaml. You should find a block that looks like the following:

<Page Include="Properties\DesignTimeResources.xaml">
    <SubType>Designer</SubType>
    <Generator>MSBuild:Compile</Generator>
</Page>

  1. 用以下内容替换第8步中的块:

<Page Include="Properties\DesignTimeResources.xaml" Condition="'$(DesignTime)'=='true' OR ('$(SolutionPath)'!='' AND Exists('$(SolutionPath)') AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true')">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
    <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>

  1. 现在重新加载/构建 CustomControls 项目.
  2. 从您的 StylesProject 向刚创建的DesignTimeResources.xaml文件添加对ResourceDictionary的引用.
  3. 重建您的解决方案.
  1. Now reload/build the CustomControls project.
  2. Add a reference to the ResourceDictionary from your StylesProject to the DesignTimeResources.xaml file you just created.
  3. Rebuild your solution.

现在您应该在 CustomControls 项目的设计器中看到样式!

You should now see the your styling in the designer of your CustomControls project!

这篇关于使用WPF自定义控件库(.NET Framework)中普通WPF项目中的App.xaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆