【问题标题】:Binding property to RelativeLayout.XConstraint in XAML Xamarin Forms在 XAML Xamarin 表单中将属性绑定到 RelativeLayout.XConstraint
【发布时间】:2020-10-05 09:13:46
【问题描述】:

我在将模型对象中的属性值绑定到 XAML 中的 RelativeLayout.XConstraint 属性时遇到问题。

我有一个模型,有一个名为public int StartLocation { get; set; } 的属性,但是如果我将此属性绑定到我的RelativeLayout.XConstraint 在一个简单的BoxView 元素上,它不会设置X 约束属性。我第一次尝试这样做

<RelativeLayout BindableLayout.ItemsSource="{Binding Broadcasts}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <BoxView BackgroundColor="PaleVioletRed" RelativeLayout.XConstraint="{Binding StartLocation}" WidthRequest="{Binding Length}" HeightRequest="60" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</RelativeLayout>

我这里有两个绑定。 StartLocation 和 Length,以及 Length Binding 工作正常,但是使用这种方法,它将 BoxView 元素 xConstraint 设置为开始(我认为它是相对 X 位置 0)。

我也试过这种方法

<RelativeLayout BindableLayout.ItemsSource="{Binding Broadcasts}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <BoxView BackgroundColor="PaleVioletRed" RelativeLayout.XConstraint="{ConstraintExpression Constant={Binding StartLocation}}" WidthRequest="{Binding Length}" HeightRequest="60" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</RelativeLayout>

运行代码时出现错误提示 无法分配属性“常量”:属性不存在,或者不可分配,或者值和属性之间的类型不匹配 em>

我希望有人可以帮助我解决这个问题。提前致谢。

【问题讨论】:

  • StartLocation 设置时是否调用 OnPropertyChanged?

标签: c# xaml xamarin xamarin.forms data-binding


【解决方案1】:

所以我一直在努力解决类似的问题。我今天设法通过一个简洁的解决方法解决了它。这是我所做的:

(Xaml)

            <RelativeLayout x:Name="aviGroup" BindingContext="this"
               RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=1, Constant=1000}"
               RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=1, Constant=25}">

                <Grid x:Name="aviGrid">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Button x:Name="Settings" Text="more" BackgroundColor="Transparent" ImageSource="{local2:ImageResource Buddy.Images.settings_icon.png}" TextColor="Transparent" Clicked="Settings_Clicked" Padding="0" HorizontalOptions="EndAndExpand" Grid.Row="0" Grid.Column="1"/>

                    <BoxView x:Name="buttonBorder" BackgroundColor="White" HeightRequest="54" WidthRequest="54" CornerRadius="24" Margin="5" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Grid.Row="0" Grid.Column="0" />

                    <ImageButton x:Name="userAvi" Source="{DynamicResource currentAVI}" Clicked="userAvi_Clicked" HorizontalOptions="Center" VerticalOptions="FillAndExpand" HeightRequest="48" WidthRequest="48" CornerRadius="22" Aspect="AspectFill" Margin="8" Grid.Row="0" Grid.Column="0"/>
                </Grid>

            </RelativeLayout>

(后面的代码)

        private void cV_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (this.Width > 100)
            {

                if (Settings.X > 0) 
                {

                    PostX = Application.Current.MainPage.Width - 300;

                    Constraint newX = Constraint.Constant(PostX);

                    RelativeLayout.SetXConstraint(aviGroup, newX);                  

                }
              
            }
        }

说明:我在调整内容视图大小时引发了一个触发器,它调用 cV_PropertyChanged。在这个方法中,我计算我想要新的 x 的位置,用这个值创建一个新的约束,然后用我想要绑定的视图和它的新值调用 RelativeLayout.SetXConstraint。

这可能不适合您的项目 1:1,但我希望您也可以使用此解决方法。

干杯!

【讨论】:

  • 谢谢。实际上,我改用了绝对布局,其中我有一个 Xamarin Forms Rectangle 类的属性,用于 AbsoluteLayout Bounds 属性。但是谢谢你的回答。也许它可以帮助其他人。
猜你喜欢
  • 2017-01-18
  • 2016-09-08
  • 2018-07-01
  • 2023-04-04
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
相关资源
最近更新 更多