首页 > 解决方案 > Xamarin.Forms - ControlTemplate 不显示 ContentPresenter

问题描述

我正在尝试使用 ControlTemplate,这样我就可以在每个页面上都有一个页脚,但是每当我使用模板时,只会显示模板并且页面内容会消失。就好像ContentPresenter没有渲染页面内容,只有模板内容。我一直在使用这个官方文档来使模板正常工作,但无济于事。

这是我的模板代码:

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestApp.App">
   <Application.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="MainFooter">
            <Grid>
                <ContentPresenter/>
                <Label Text="Template footer" FontSize="24"/>
            </Grid>
        </ControlTemplate>
    </ResourceDictionary>
   </Application.Resources>
</Application>

和页面代码:

<ContentPage Title="Test" xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="TestApp.Login">
    <ContentView ControlTemplate="{StaticResource MainFooter}" Padding="0, 20, 0, 0">

        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="Page content" HorizontalOptions="Center"/>
        </StackLayout>


    </ContentView>
</ContentPage>

从这段代码的输出中可以看出,只有 ContentTemplate 被显示,而不是“页面内容”标签(ContentPresenter 内容)。如何显示页面内容以及模板内容?

页面输出

标签: androidiosxamarinxamarin.forms

解决方案


在我的头撞墙一段时间后,一旦我在模拟器中启动应用程序,似乎页面内容实际上正在呈现。

应用ControlTemplate 后,预览器仅显示 ControlTemplate,而不显示页面内容。很可能是 Visual Studio 中的一个错误,因此进行了一些挖掘:

比较老的Bugzilla,同样的问题

其他人有同样的问题

我正在使用 Visual Studio 7.7.4 和 Xamarin 5.2.1.15。猜猜我只需要在我想预览我的页面时关闭 ControlTemplate。


推荐阅读