如何解决在XAML Xamarin表单中将属性绑定到RelativeLayout.XConstraint
我无法将模型对象中的属性值绑定到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>
运行代码 无法分配属性“常量”时,这里出现错误:属性不存在或不可分配,或者值与属性之间的类型不匹配
我希望有人可以帮助我解决这个问题。预先感谢。
解决方法
所以一段时间以来,我一直在努力解决类似的问题。今天,我设法通过一种巧妙的解决方法解决了该问题。这是我所做的:
(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,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,但我希望您也可以使用此替代方法。
干杯!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。