【问题标题】:DataTemplateSelector not found in merged resource dictionary在合并的资源字典中找不到 DataTemplateSelector
【发布时间】:2021-12-31 14:36:03
【问题描述】:

我在名为 DataTemplates.xaml 的单独文件中将 CustomDataTemplateSelector 添加到 ResourceDictionary,随后我将其链接到 App.xaml 中

当我运行应用程序时,我得到 XamlParseException 说: 键入 local:CustomDataTemplateSelector not found in xmlns clr-namespace:MyProject

另一方面,当我将 DataTemplates.xaml 的内容放到 App.xaml 中时,它工作得很好。

App.xaml

<Application.Resources>
<ResourceDictionary>
    <ResourceDictionary Source="../DataTemplates.xaml"/>

DataTemplates.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"
xmlns:local="clr-namespace:MyProject">

<DataTemplate x:Key="FirstCell">
    <Label Text="Some text"/>
</DataTemplate>

<DataTemplate x:Key="SecondCell">
        <Label Text="Some other text"/>
</DataTemplate>

<local:CustomDataTemplateSelector
    x:Key="CustomDataTemplateSelector"
    SecondTemplate="{StaticResource SecondCell}"
    FirstTemplate="{StaticResource FirstCell}"/>

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我已确认问题。 (为其他人澄清 - 尝试运行应用程序时发生解析错误。在 App.InitializeComponent 期间。)我在 App.xaml 和 DataTemplates.xaml 上都有 xmlns:local ...。

    要解决此问题,请使用替代语法来合并字典,ResourceDictionary.MergedDictionaries

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
                 xmlns:local="clr-namespace:XFSOAnswers"
                 x:Class="XFSOAnswers.App">
        <Application.Resources>
            <ResourceDictionary>
                
                <Style TargetType="ContentPage" ApplyToDerivedTypes="True">
                    <Setter Property="ios:Page.UseSafeArea" Value="True"/>
                </Style>
    
                <ResourceDictionary.MergedDictionaries>
                    <local:DataTemplates />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    如果它很重要,这就是我对 DataTemplates 的看法:

    DataTemplates.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"
    xmlns:local="clr-namespace:XFSOAnswers">
    
        <DataTemplate x:Key="FirstCell">
            <Label Text="Some text"/>
        </DataTemplate>
    
        <DataTemplate x:Key="SecondCell">
            <Label Text="Some other text"/>
        </DataTemplate>
    
        <local:CustomDataTemplateSelector
        x:Key="CustomDataTemplateSelector"
        SecondTemplate="{StaticResource SecondCell}"
        FirstTemplate="{StaticResource FirstCell}"/>
    
        </ResourceDictionary>
    

    DataTemplates.xaml.cs:

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace XFSOAnswers
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class DataTemplates : ResourceDictionary
        {
            public DataTemplates()
            {
            }
        }
    }
    

    (原来Source的语法应该不需要这个.cs文件。不知道我用的语法是否需要。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多