Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Xamarin XAML中,如何使用样式在RelativeLayout上设置约束?_Xaml_Xamarin.forms - Fatal编程技术网

在Xamarin XAML中,如何使用样式在RelativeLayout上设置约束?

在Xamarin XAML中,如何使用样式在RelativeLayout上设置约束?,xaml,xamarin.forms,Xaml,Xamarin.forms,我正在努力计算XAML语法,以便使用样式将约束应用于相对性视图 下面的第一段Xamarin XAML显示了一对嵌套的RelativeLayout元素,用于构建简单布局(内部元素只是在可以添加其他内容的区域周围放置一个边距)。此版本的代码在iOS和Android上构建并运行良好 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

我正在努力计算XAML语法,以便使用
样式
将约束应用于
相对性视图

下面的第一段Xamarin XAML显示了一对嵌套的
RelativeLayout
元素,用于构建简单布局(内部元素只是在可以添加其他内容的区域周围放置一个边距)。此版本的代码在iOS和Android上构建并运行良好

<?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="App2.Page1">
    <RelativeLayout BackgroundColor="Gray">
        <RelativeLayout BackgroundColor="Maroon"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.9,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.9,Constant=0}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.05,Constant=0}"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.05,Constant=0}">
            <BoxView Color="Yellow"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"/>
        </RelativeLayout>
    </RelativeLayout>
</ContentPage>
有谁能帮我解释一下这样做的语法吗

这是一个完整示例的链接(显然需要安装Xamarin,并且需要还原nuget软件包):

尝试以下方法:

<ResourceDictionary>
    <Style x:Key="LayoutStyle" TargetType="RelativeLayout">
        <Setter Property="BackgroundColor" Value="Maroon"/>
        <Setter Property="RelativeLayout.HeightConstraint" Value="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.9,Constant=0}"/>
        <Setter Property="RelativeLayout.WidthConstraint" Value="{ConstraintExpression RelativeToParent,Property=Width,Factor=0.9,Constant=0}"/>
        <Setter Property="RelativeLayout.YConstraint" Value="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.05,Constant=0}"/>
        <Setter Property="RelativeLayout.XConstraint" Value="{ConstraintExpression RelativeToParent,Property=Width,Factor=0.05,Constant=0}"/>
    </Style>
</ResourceDictionary>


谢谢。这确实是建立和运行的;但是,我认为Xamarin布局模型中可能存在一个bug,因为当我将约束移动到样式中时,会出现不一致的行为。它不像CSS,是不是,将规则移动到样式中会降低其在决定应用什么的算法中的分数?我将把它作为Xamarin的一个bug提出来。通过在元素上提供不同的内联设置,可以覆盖样式中设置的属性,但是规则比CSS简单得多。然而,Xamarin.Forms对于没有为布局指定足够的信息相当敏感,这表现为不一致的行为。由于此答案针对原始问题,请将其标记为已接受答案。为了弄清楚页面的布局是怎么回事,除了向Xamarin提交一个bug,你还可以用你的页面的两个版本的截图问另一个问题。我错了。我可以踢自己。当尝试应用如上所示的样式时,我忘记将“{StaticResource…}”放在说明符中。因此,100%清楚地说,@DavidS的答案是完全正确的,并且非常有效。再次感谢你。
<ResourceDictionary>
    <Style x:Key="LayoutStyle" TargetType="RelativeLayout">
        <Setter Property="BackgroundColor" Value="Maroon"/>
        <Setter Property="RelativeLayout.HeightConstraint" Value="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.9,Constant=0}"/>
        <Setter Property="RelativeLayout.WidthConstraint" Value="{ConstraintExpression RelativeToParent,Property=Width,Factor=0.9,Constant=0}"/>
        <Setter Property="RelativeLayout.YConstraint" Value="{ConstraintExpression RelativeToParent,Property=Height,Factor=0.05,Constant=0}"/>
        <Setter Property="RelativeLayout.XConstraint" Value="{ConstraintExpression RelativeToParent,Property=Width,Factor=0.05,Constant=0}"/>
    </Style>
</ResourceDictionary>