C# ContentPresenter未在ControlTemplate Xamarin表单中显示内容
我想创建一个可重用的模板页面,以便在整个应用程序中使用 我决定使用C# ContentPresenter未在ControlTemplate Xamarin表单中显示内容,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我想创建一个可重用的模板页面,以便在整个应用程序中使用 我决定使用ControlTemplate来实现这一点,但我遇到了一些问题。第一个问题是,当我绑定到标题文本时,它不会显示。第二个问题是,使用此ControlTemplate的页面的子项不可见,ContentPresenter为空 控制模板: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http:/
ControlTemplate
来实现这一点,但我遇到了一些问题。第一个问题是,当我绑定到标题文本时,它不会显示。第二个问题是,使用此ControlTemplate的页面的子项不可见,ContentPresenter为空
控制模板:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.MainViewTemplate">
<ContentPage.ControlTemplate>
<ControlTemplate>
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label TextColor="Black"
Text="{TemplateBinding HeaderText}"
BackgroundColor = "Green"
Grid.Row="0"/>
<ContentPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</ContentPage.ControlTemplate>
使用它的页面:
<template:MainViewTemplate xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.MainView"
xmlns:template="clr-namespace:Project"
HeaderText="Main view">
<StackLayout Margin="10"
BackgroundColor="Green">
<Label Text = "Test"/>
</StackLayout>
</template:MainViewTemplate>
绑定HeaderText
的标签未显示其文本,如果我将其更改为写入硬编码文本,则其工作正常,因此绑定出现问题。其次,我也能看到绿色的背景。其次,contentpresenter没有在使用此ControlTemplate的页面内显示内容。我看不出您在代码中正确使用了模板,我提供了一个示例,您可以查看:
ControlTemplate,有一个控件x:key=“template1”
使用ControlTemplate x:key=”“模板
截图:
有关ControlTemplate的更多详细信息,请查看:
嘿,我看你发布的代码没有任何问题。你是在安卓还是iOS上运行这个?iOS,但还没有在安卓@pinedax上测试。现在试试这个。我记得我也尝试过使用content.resources,但没有成功。我会再看。@DiddanDo是的,不记得在代码中设置ControlTemplate=“{StaticResource template1}”。是的,我知道。我现在才看到,但是现在我也看不到上面的内容。不知道为什么它不起作用。我正在逐行跟踪您的代码。找不到正确的键?现在,它没有显示模板或页面本身的内容。我的代码显示了模板顶部的代码。
<template:MainViewTemplate xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.MainView"
xmlns:template="clr-namespace:Project"
HeaderText="Main view">
<StackLayout Margin="10"
BackgroundColor="Green">
<Label Text = "Test"/>
</StackLayout>
</template:MainViewTemplate>
<ContentPage
x:Class="demo3.template.MainViewTemplate"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentPage.Resources>
<ControlTemplate x:Key="template1">
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label
Grid.Row="0"
BackgroundColor="Green"
Text="{TemplateBinding HeaderText}"
TextColor="Black" />
<ContentPresenter Grid.Row="1" />
</Grid>
</ControlTemplate>
</ContentPage.Resources>
public partial class MainViewTemplate : ContentPage
{
public static readonly BindableProperty HeaderTextProperty = BindableProperty.Create(nameof(HeaderText), typeof(string), typeof(MainViewTemplate), default(string));
public string HeaderText
{
get => (string)GetValue(HeaderTextProperty);
set => SetValue(HeaderTextProperty, value);
}
public MainViewTemplate()
{
InitializeComponent();
}
}
<control:MainViewTemplate
x:Class="demo3.template.Page1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:demo3.template"
ControlTemplate="{StaticResource template1}"
HeaderText="Main view">
<ContentPage.Content>
<StackLayout Margin="10" BackgroundColor="Green">
<Label Text="Test" />
</StackLayout>
</ContentPage.Content>
</control:MainViewTemplate>