1

不是Xamarin Forms 的副本 没有为“Sku”找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配

我对 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.

我哪里错了?

4

3 回答 3

1

我基本上想要做的是将标签的高度设置为渲染设备高度的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替换你的父布局

于 2020-09-22T07:55:07.313 回答
0

您正在放入LabelStackLayout 而您使用的值 like{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>
于 2020-09-22T05:44:27.583 回答
0

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

于 2020-09-22T06:13:41.443 回答