首页 > 解决方案 > 在 XAML UWP 中使用另一个文件夹中的字典

问题描述

这是我的字典 [Dictionaries/myDictionary.xaml] 的内容:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:myApp/Dictionaries"
    xmlns:media="using:Windows.UI.Xaml.Media">

    <ResourceDictionary x:Key="Default">
        <AcrylicBrush x:Key="myAcrylicBrush"
            BackgroundSource="HostBackdrop"
            TintColor="#202020"
            TintOpacity="0.8"
            FallbackColor="#202020"/>
    </ResourceDictionary>

</ResourceDictionary>

这里是我需要使用它的地方 [Pages/myPage]:

<Page
    x:Class="myApp.Pages.myPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:myApp.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:animations="using:Microsoft.Toolkit.Uwp.UI.Animations"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Here it gives me error -->
                <ResourceDictionary Source="pack://application:,,,/ReferencedAssembly;component/Dictionaries/myDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="myStyle" TargetType="Grid">
                <Setter Property="Background" Value="{StaticResource myAcrylicBrush}"/>
            </Style>

        </ResourceDictionary>
    </Page.Resources>

    <Grid x:Name="gridPrincipale" Style="{StaticResource myStyle}">

    </Grid>

</Page>

我试过了

Source="pack://application:,,,/ReferencedAssembly;component/Dictionaries/myDictionary.xaml"

但它不起作用……</p>

文件夹“Dictionaries”和“Pages”都在项目的文件夹中。

标签: xamluwpwindows-10-universal

解决方案


对于 UWP,您需要使用源的ms-appx位置。

例子:

<ResourceDictionary Source="ms-appx:///Company.Themes/Dictionaries/myDictionary.xaml" />

Company.Themes项目名称在哪里,是项目Dictionaries/myDictionary.xaml根目录下资源文件的位置


推荐阅读