You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms重复使用自定义模板触发XamlParseException问题咨询

Xamarin.Forms XamlParseException When Reusing Custom Template in CarouselPage

我最近也碰到过类似的XAML解析问题,结合你的描述和代码来看,这个异常表面是“找不到类型”,但实际原因可能和CarouselPage的子项要求、命名空间解析或者自定义视图的访问级别有关。咱们一步步拆解:

问题场景还原

你基于GrialKit创建引导页,在WalkthroughPage(一个CarouselPage)中重复使用自定义的WalkthroughStepItemTemplate,第一次实例模板正常,但第二次、第三次调用时触发XamlParseException,提示找不到该类型。你已经尝试修改命名空间引用、调整Linker Behavior,但都没解决问题。你的XAML代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
              x:Class="WhoseKitchenApp.Views.Walkthroughs.WalkthroughPage" 
              xmlns:artina="clr-namespace:UXDivers.Artina.Shared;assembly=UXDivers.Artina.Shared" 
              xmlns:local="clr-namespace:WhoseKitchenApp;assembly=WhoseKitchenApp">
<!-- following line is line 9 -->
<local:Views.Walkthroughs.Templates.WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 1" Text="Welcome slide 1" IconColor="#5F7DD4" ButtonBackgroundColor="#5F7DD4" ButtonText="Next" />
<!-- following line is line 11 -->
<local:Views.Walkthroughs.Templates.WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 2" Text="Hello slide 2" IconColor="#7C4ECD" ButtonBackgroundColor="#7C4ECD" ButtonText="Next" />
<local:Views.Walkthroughs.Templates.WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 3" Text="Last slide, slide 3" IconColor="#E51E61" ButtonBackgroundColor="#E51E61" ButtonText="Finish" />
</CarouselPage>

可能的原因及解决办法

1. CarouselPage的子项类型不符合要求(最可能的根源)

CarouselPage的直接子元素必须是ContentPage类型,如果你的WalkthroughStepItemTemplate是继承自ContentView或其他非ContentPage的视图,XAML解析器第一次可能因为缓存或宽松校验通过,但后续实例化时会严格检查类型,最终抛出“找不到类型”的异常(这是XAML解析器的一个常见误导性错误提示)。

解决办法是把每个模板包裹在ContentPage中:

<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
              x:Class="WhoseKitchenApp.Views.Walkthroughs.WalkthroughPage" 
              xmlns:artina="clr-namespace:UXDivers.Artina.Shared;assembly=UXDivers.Artina.Shared" 
              xmlns:templates="clr-namespace:WhoseKitchenApp.Views.Walkthroughs.Templates;assembly=WhoseKitchenApp">

    <ContentPage>
        <templates:WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 1" Text="Welcome slide 1" IconColor="#5F7DD4" ButtonBackgroundColor="#5F7DD4" ButtonText="Next" />
    </ContentPage>
    <ContentPage>
        <templates:WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 2" Text="Hello slide 2" IconColor="#7C4ECD" ButtonBackgroundColor="#7C4ECD" ButtonText="Next" />
    </ContentPage>
    <ContentPage>
        <templates:WalkthroughStepItemTemplate BackgroundColor="#FF6600" Header="Header text 3" Text="Last slide, slide 3" IconColor="#E51E61" ButtonBackgroundColor="#E51E61" ButtonText="Finish" />
    </ContentPage>
</CarouselPage>

2. 修正命名空间引用,避免嵌套路径歧义

你之前用local:Views.Walkthroughs.Templates.WalkthroughStepItemTemplate这种嵌套路径,容易让XAML解析器混淆命名空间层级。建议直接给模板所在的命名空间起一个简洁的别名(比如templates),这样引用更清晰,也能避免解析错误:

xmlns:templates="clr-namespace:WhoseKitchenApp.Views.Walkthroughs.Templates;assembly=WhoseKitchenApp"

然后用templates:WalkthroughStepItemTemplate来引用你的模板,就像上面的代码那样。

3. 确保自定义模板类是public访问级别

如果你的WalkthroughStepItemTemplate类不是public的,XAML解析器在第一次实例后,后续实例可能无法访问到该类型,导致找不到类型的错误。检查你的C#代码:

namespace WhoseKitchenApp.Views.Walkthroughs.Templates
{
    public class WalkthroughStepItemTemplate : ContentView // 假设继承自ContentView
    {
        // 你的模板逻辑代码
    }
}

一定要确保类的访问修饰符是public。

4. 清理编译缓存,重建项目

有时候XAML的编译缓存会导致奇怪的解析问题,试试这些步骤:

  • 清理解决方案(Clean Solution)
  • 手动删除项目的bin和obj文件夹
  • 重建解决方案(Rebuild Solution)
  • 重启你的开发工具(Visual Studio/Xamarin Studio)

总结

优先检查CarouselPage的子项类型问题,这是最可能导致你这个异常的原因。按照上面的步骤调整后,应该能解决重复使用模板时的解析错误。

内容的提问来源于stack exchange,提问作者Jules

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:24:12