【问题标题】:Xamarin.Forms - ControlTemplate doesn't display ContentPresenterXamarin.Forms - ControlTemplate 不显示 ContentPresenter
【发布时间】:2019-02-15 10:08:25
【问题描述】:

我正在尝试使用 ControlTemplate,以便在每个页面上都有一个页脚,但是每当我使用模板时,显示模板并且页面内容消失。就好像ContentPresenter 不呈现页面内容,只呈现模板内容。我一直在使用this official doc 来使模板正常工作,但无济于事。

这是我的模板代码:

<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 内容)。如何显示页面内容和模板内容?

【问题讨论】:

    标签: android ios xamarin xamarin.forms


    【解决方案1】:

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

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

    Relatively old Bugzilla, same problem

    Someone else with the same problem

    我正在使用 Visual Studio 7.7.4 和 Xamarin 5.2.1.15。我想只要我想预览我的页面,我就必须关闭 ControlTemplate。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多