简体   繁体   English

ContentPresenter 未显示 ControlTemplate Xamarin Forms 内的内容

[英]ContentPresenter not showing content inside ControlTemplate Xamarin Forms

I want to create a reusable template page that I want to use throughout my app.我想创建一个可重用的模板页面,我想在整个应用程序中使用它。

I decided to go with a ControlTemplate to achieve this but I am having issues.我决定用ControlTemplate go 来实现这一点,但我遇到了问题。 The first issue is that the HeaderText does not get displayed when I bind to it.第一个问题是当我绑定到HeaderText时没有显示。 The second issue is that the Page using this ControlTemplate does not have his children visible, the ContentPresenter is empty.第二个问题是使用这个 ControlTemplate 的 Page 没有他的孩子可见, ContentPresenter 是空的。

ControlTemplate:控制模板:

<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>

And its code behind:及其背后的代码:

public partial class MainViewTemplate : ContentPage
{
    public static readonly BindableProperty HeaderTextProperty = BindableProperty.Create(nameof(HeaderText), typeof(string), typeof(MainViewTemplate), default(string));

    public string HeaderText
    {
        get { return (string)GetValue(HeaderTextProperty); }
        set { SetValue(HeaderTextProperty, value); }
    }

    public MainViewTemplate()
    {
        InitializeComponent();
    }
}

Page that uses it:使用它的页面:

<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>

The label that binds HeaderText is not showing its text, if i change it to write hardcoded text it works, so something is off with the binding.绑定HeaderText的 label 没有显示其文本,如果我将其更改为编写硬编码文本,它可以工作,所以绑定有些问题。 Secondly, I can see the green background as well.其次,我也可以看到绿色背景。 Secondly, the contentpresenter is not showing the content inside the page that uses this ControlTemplate.其次,contentpresenter 没有显示使用此 ControlTemplate 的页面内的内容。

I don't see you use Template in your code correctly, I do one sample that you can take a look:我没有看到您在代码中正确使用模板,我做了一个示例,您可以看看:

ControlTemplate, there one control x:key="template1" ControlTemplate,有一个控件 x:key="template1"

<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();
    }
}

Using ControlTemplate x:key=""template使用 ControlTemplate x:key=""template

<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>

The screenshot:截图:

在此处输入图像描述

More detailed info about ControlTemplate, please take a look:有关 ControlTemplate 的更多详细信息,请查看:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-template#substitute-content-into-a-contentpresenter https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-template#substitute-content-into-a-contentpresenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM