簡體   English   中英

ContentPresenter 未顯示 ControlTemplate Xamarin Forms 內的內容

[英]ContentPresenter not showing content inside ControlTemplate Xamarin Forms

我想創建一個可重用的模板頁面,我想在整個應用程序中使用它。

我決定用ControlTemplate go 來實現這一點,但我遇到了問題。 第一個問題是當我綁定到HeaderText時沒有顯示。 第二個問題是使用這個 ControlTemplate 的 Page 沒有他的孩子可見, 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>

及其背后的代碼:

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

使用它的頁面:

<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的 label 沒有顯示其文本,如果我將其更改為編寫硬編碼文本,它可以工作,所以綁定有些問題。 其次,我也可以看到綠色背景。 其次,contentpresenter 沒有顯示使用此 ControlTemplate 的頁面內的內容。

我沒有看到您在代碼中正確使用模板,我做了一個示例,您可以看看:

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

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

截圖:

在此處輸入圖像描述

有關 ControlTemplate 的更多詳細信息,請查看:

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