我正在尝试使用 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 内容)。如何同时显示页面内容和模板内容?
最佳答案
在我用头撞墙一段时间后,一旦我在模拟器中启动应用程序,页面内容似乎就会被渲染。
应用 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。
关于android - Xamarin.Forms - ControlTemplate 不显示 ContentPresenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706933/