【问题标题】:No property, BindableProperty, or event found for "HeightRequest", or mismatching type between value and property error in Xamarin.Forms未找到“HeightRequest”的属性、BindableProperty 或事件,或者 Xamarin.Forms 中的值和属性错误之间的类型不匹配
【发布时间】:2020-09-22 04:51:16
【问题描述】:

不是Xamarin Forms No property, bindable property, or event found for 'Sku', or mismatching type between value and property的重复项

我对 Xamarin.Forms 完全陌生,正在尝试我的第一个 Hello World 应用程序。
我写了以下代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="hello_world.MainPage">
    <StackLayout>
        <Label 
            Text="Welome" 
            BackgroundColor="Yellow" 
            TextColor="Green" 
            HeightRequest="{ConstraintExpression Type=RelativeToView, Factor=0.05, Constant=0}"
            FontSize="Medium" />
    </StackLayout>

</ContentPage>

但是,我收到了错误

No property, BindableProperty, or event found for "HeightRequest", or mismatching type between value and property.

我哪里错了?

【问题讨论】:

    标签: xamarin.forms compiler-errors


    【解决方案1】:

    您将Label 放入StackLayout 中,而您使用的值(如{ConstraintExpression Type=RelativeToView, Factor=0.05, Constant=0})用于relativelayout

    这是两种不同的布局,不能混用。阅读文档并查看其中的示例以了解如何使用它们。

    如果您有任何问题,请随时问我:)。

    更新代码:

    <RelativeLayout>
        <Label 
            Text="Welome" 
            BackgroundColor="Yellow" 
            TextColor="Green" 
            FontSize="Medium"
    
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
    
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView, Factor=0.05,Property= Height, Constant=0}"
             />
    </RelativeLayout>
    

    【讨论】:

    • 嗯,这甚至​​可能是一个 XY 问题。我基本上想要做的是将标签的高度设置为渲染设备高度的5%
    • @d4rk4ng31 你应该使用RelativeLayout,看我更新的代码。
    • 我实际上不记得确切的帖子(这里的某处),但那里有一条经过充分验证的评论说尽量避免相对布局。
    • 如果你想实现set the height of the label to 5% of the rendering device's height,RelativeLayout是比其他更好的选择。
    • 每种布局都有优点和缺点。
    【解决方案2】:

    我基本上想要做的是将标签的高度设置为 5% 渲染设备的高度

    好的,如果这只是您关心的问题,那么可以这样使用它:

    <RelativeLayout>
        <Label 
            Text="Welome" 
            BackgroundColor="Yellow" 
            TextColor="Green"
            RelativeLayout.HeightConstraint="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.05,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.05,Constant=0}"
            FontSize="Medium" />
    </RelativeLayout>
    

    您面临的异常是由于语法错误。你必须用 RelativeLayout 而不是 StackLayout

    替换你的父布局

    【讨论】:

      【解决方案3】:

      请原谅HeightRequest 是可绑定属性。 你的情况是“值和属性的类型不匹配”,只能是double,不能绑定到ConstraintExpression

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2019-03-28
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多