如果在使用模板的XAML中没有指定HeightRequest,如何使模板内部的HeightRequest被忽略?

4

这个模板以HeaderHeight作为绑定参数。如果在调用模板的XAML中没有指定HeaderHeight,有没有办法让StackLayout忽略它呢?

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Japanese;assembly=Japanese" x:Class="Japanese.Templates.HeaderTemplate" x:Name="this">
    <ContentView.Triggers>
        <Trigger TargetType="local:Templates.HeaderTemplate" Property="HeaderType" Value="Custom">
            <Setter Property="Content">
                <Setter.Value>
                    <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="0" Margin="0">
                        <StackLayout HeightRequest="{Binding HeaderHeight, Source={x:Reference this}}" Orientation="Vertical" Spacing="0" Margin="0" >
                            <Label Text="ABC" HorizontalOptions="Start" VerticalOptions="EndAndExpand" />
                        </StackLayout>
                    </StackLayout>
                </Setter.Value>
            </Setter>
        </Trigger>
    </ContentView.Triggers>
</ContentView>

1
为什么不直接将其设置为0?如果您想忽略它,可以在您的视图模型中添加一个条件。 - jbtamares
1个回答

3

您可以将HeaderHeight的默认值设置为-1

UI应该恢复正常

HeaderHeight = -1;

一旦它成为一个 BindableProperty,它应该看起来像这样:
public static readonly BindableProperty HeaderHeightProperty = BindableProperty.Create(nameof(HeaderHeight), typeof(double), typeof(HeaderTemplate), -1);
public double HeaderHeight
{
    get => (double)GetValue(HeaderHeightProperty);
    set => SetValue(HeaderHeightProperty, value);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接