【问题标题】:Stand alone ResourceDictionary resource not found未找到独立 ResourceDictionary 资源
【发布时间】:2021-06-08 13:09:50
【问题描述】:

我关注了https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries#stand-alone-resource-dictionaries,但我得到了错误。

XFC0124 找不到资源“ResourceDictionary1.xaml”。

我的代码是

ResourceDictionary1.xaml(已删除代码隐藏)编译选项:嵌入资源

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

 <x:String x:Key="test">
        test string
    </x:String>

</ResourceDictionary>

ContentPage1.xaml

   <ContentPage.Resources>
        <ResourceDictionary Source="ResourceDictionary1.xaml"/>
  </ContentPage.Resources>

【问题讨论】:

    标签: xaml xamarin.forms resourcedictionary


    【解决方案1】:

    我使用链接中提供的步骤进行测试,一切正常。

    检查以下步骤

    1. 添加一个ContentView 文件并将其重命名为 ResourceDictionary1 。

    2. 删除代码隐藏

    3. 删除 xaml 中的 x:Class="FormsApp.ResourceDictionary1" 并将标记从 ContentView 更改为 ResourceDictionary

    <?xml version="1.0" encoding="UTF-8"?>
    <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" >
        
        
        <x:String x:Key="test">
            test string
        </x:String>
    </ResourceDictionary>
    
    1. 在其他 xaml 中的使用
    
        <ContentPage.Resources>
            <ResourceDictionary Source="ResourceDictionary1.xaml"/>
        </ContentPage.Resources>
        
        
        <ContentPage.Content>
            <StackLayout>
                <Button Clicked="Button_Clicked" Text="{StaticResource test}"/>
            </StackLayout>
        </ContentPage.Content>
    


    如果你做了以上所有步骤,问题仍然存在,那一定是文件路径有问题。

    ResourceDictionary 文件应与使用 ResourceDictionary 的页面位于同一级别。

    • 我们可以将它们放在表单项目的根目录中。

    • 我们可以将它们放在同一个文件夹中。

    • 如果不在同一级别,我们应该在页面中设置相对文件路径。

       // in page4.xaml
        <ContentPage.Resources>
            <ResourceDictionary Source="MyFolder/ResourceDictionary1.xaml"/>
        </ContentPage.Resources>
    

    【讨论】:

    • 非常感谢它不在同一个文件路径中,我试图放置一个绝对路径而不是相对路径。
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2020-09-07
    • 1970-01-01
    • 2023-04-08
    • 2015-01-08
    • 1970-01-01
    • 2019-04-30
    • 2013-06-22
    相关资源
    最近更新 更多