此XAML的C#等效项是什么?

时间:2019-03-10 14:20:43

标签: c# xamarin relativelayout positioning

我正在尝试在Xamarin应用程序(C#)后面的代码中添加此Xaml,但我还没有找到一个很好的示例来说明如何做。 这是Xaml

<Image x:Name="TargetImage" 
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=ZXingScannerView1, Property=Y, Constant={StaticResource TargetYConstant}}" 
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=ZXingScannerView1, Property=X, Constant={StaticResource TargetXConstant}, Factor=0.80}">

其中TargetYConstant和TargetXConstant是App.Xaml中ResourceDictionary键中的变量 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我做了一个有关RelativeLayout的示例,您可以看一下:

  <RelativeLayout x:Name="layout">
        <BoxView
            x:Name="redBox"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                   Property=Height,
                                                                   Factor=.8,
                                                                   Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                  Property=Width,
                                                                  Factor=1,
                                                                  Constant=0}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
                                                              Property=Height,
                                                              Factor=.15,
                                                              Constant=0}"
            Color="Red" />

        <BoxView x:Name="blueBox" Color="Blue" />
    </RelativeLayout>


public Page5 ()
    {
        InitializeComponent ();

        double valuex = (double)Application.Current.Resources["TargetXConstant"];
        double valuey = (double)Application.Current.Resources["TargetYConstant"];

        **layout.Children.Add(blueBox, Constraint.RelativeToView(redBox, (Parent, sibling) => {
            return sibling.X + valuex;
        }), Constraint.RelativeToView(redBox, (parent, sibling) => {
            return sibling.Y + valuey;
        }), Constraint.RelativeToParent((parent) => {
            return parent.Width * .5;
        }), Constraint.RelativeToParent((parent) => {
            return parent.Height * .5;
        }));**
    }

资源位于App.xaml中:

<Application.Resources>
    <ResourceDictionary>
        <x:Double x:Key="TargetXConstant">40</x:Double>
        <x:Double x:Key="TargetYConstant">40</x:Double>
    </ResourceDictionary>
</Application.Resources>

粗体部分等同于xaml:

<BoxView
            x:Name="blueBox"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                   Property=Height,
                                                                   Factor=.5,
                                                                   Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                  Property=Width,
                                                                  Factor=.5,
                                                                  Constant=0}"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
                                                              ElementName=redBox,
                                                              Property=X,
                                                              Factor=1,
                                                              Constant={StaticResource TargetXConstant}}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,
                                                              ElementName=redBox,
                                                              Property=Y,
                                                              Factor=1,
                                                              Constant={StaticResource TargetYConstant}}"
            Color="Blue" />

您还可以查看RelativeLayout:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relative-layout