Maui 使用类库中的 style.xaml

问题描述 投票:0回答:1

我在毛伊岛创建了 2 个项目。第一个是一个类库,其中包含资源字典。第二个是我想使用资源字典的实际应用程序,因此我尝试在

App.xaml
中引用它。 我已将 GuiLibrary 设置为需要使用它的应用程序的依赖项 所有尝试都在
App.xaml

的应用程序部分的顶部
 xmlns:gui="clr-namespace:AppGuiLibrary;assembly=AppGuiLibrary"

我尝试了以下不同的组合,但没有成功。

  1. <ResourceDictionary Source="AppGuiLibrary/Styles.xaml"/>
  2. <ResourceDictionary Source="/AppGuiLibrary;component/Styles.xaml"/>
  3. <ResourceDictionary Source="/gui;component/Styles.xaml"/>

Styles.xaml
是 Maui.Xaml 内容类型。

我得到的所有错误是

未找到资源 X

有谁知道我缺少什么才能让它发挥作用?

xaml maui
1个回答
0
投票

假设您有一个名为“BaseClassesMAUI”的库,您在其中定义了资源字典“Styles/Styles.xaml”和“Styles/Colors.xaml”,如下所示

<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="BaseClassesMAUI.Styles.Colors">
<Color x:Key="PrimaryBackgroundColor_Dark">#282d37</Color>
<Color x:Key="PrimaryBackgroundColor_Light">#182d37</Color></ResourceDictionary>

将这些资源字典包含到您的应用程序中的正确方法:

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:styles="clr-namespace:BaseClassesMAUI.Styles;assembly=BaseClassesMAUI"
         xmlns:local="clr-namespace:LocalProj"
         x:Class="LocalProj.App">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <styles:Colors />
            <styles:Styles />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

请注意,ResourceDictionary 必须具有已定义的 x:Class 属性,以便我们可以引用它。

© www.soinside.com 2019 - 2024. All rights reserved.