xamarin - 如果未在使用模板的 XAML 中指定,我怎样才能使模板内的 HeightRequest 被忽略?

标签 xamarin xamarin.forms

此模板将 HeaderHeight 作为绑定(bind)参数。如果在调用模板的 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>

最佳答案

您可以设置默认值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);
}

关于xamarin - 如果未在使用模板的 XAML 中指定,我怎样才能使模板内的 HeightRequest 被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52130529/

相关文章:

xamarin.forms - 在 xamarin.form 应用程序中存储 token 的位置

c# - Xamarin ListView 图像在滚动上消失

c# - 在 Xamarin.Forms 中处理 WebView 页面的正确方法是什么?

c# - 如何更改 Xamarin.Forms 中的页面?

android - 为什么 Android 设备上 TabbedPage 中的页面标题默认为大写

java - 如何创建 Horizo​​ntalListView?

c# - 在 Xamarin Forms 中渲染 HTML 并从 HTML 链接打开嵌入的 PDF

ios - Xamarin.Forms Foundation.MonoTouchException : Objective-C exception thrown. 名称: NSInternalInconsistencyException

mvvm - 项目不会出现在ListView Xamarin中。为什么?

c# - Rest 异常 : Unable to find a constructor to use for type "myclass". 一个类应该有一个默认构造函数