什么是这个XAML的C#等价物?

问题描述 投票:0回答:1

我正在尝试在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键中的变量任何想法?

c# xamarin relativelayout positioning
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.