如何使用引用的 class 库中的控件,该库使用自己的 style.xaml 文件

How to use Controls from refrenced class library which uses own style.xaml file

所以我构建了一个 class 库(通用 windows) 以便在尽可能多的应用程序中重用我的所有控件。

我已经编写了所有控件,现在我想使用它们,但是当我在实际应用程序中使用这些控件时,Designer 出现故障,我无法构建应用程序。

这是错误信息:

这只是意味着找不到我的样式文件。

所以我的解决方案结构如下:

示例控件中使用的样式如下:

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="/Styles/SampleStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

这完全有效,但是当我像这样在主页中使用控件时

<Page
    x:Class="SandBoxSampleApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SandBoxSampleApp"
    xmlns:SampleControls="pack://application:,,,/SandBoxClassLibrary:component/Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:UsingSampleControls="using:SandBoxClassLibrary.Controls"
    >
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <UsingSampleControls:SampleUserControl/>
    </Grid>
</Page>

设计师坏了等等,看了很多文章,好像没看懂。即使我在互联网上看到我必须像这样访问控件时,InteliSense 也设法识别控件等等

xmlns:SampleControls="pack://application:,,,/SandBoxClassLibrary:component/Controls"

这是我的示例项目: SampleProject

作为文件 UserControl usage scope,

A UserControl element has a special situation for resource-lookup behavior because it has the inherent concepts of a definition scope and a usage scope. A UserControl that makes a XAML resource reference from its definition scope must be able to support the lookup of that resource within its own definition-scope lookup sequence—that is, it cannot access app resources. From a UserControl usage scope, a resource reference is treated as being within the lookup sequence towards its usage page root (just like any other resource reference made from an object in a loaded object tree) and can access app resources.

所以你应该把SampleStyle.xaml放在UserControl的同根位置,也就是说SampleStyle.xaml 文件应放入您的 Controls 文件夹。