大佬教程收集整理的这篇文章主要介绍了使用 MVVM 在 Xamarin.Forms 中单击时如何更改 Frame 控件的颜色,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这个问题已经被问过很多次了,但我一直无法使用其他问题中提供的解决方案来解决这个问题。
问题来了: 我想在单击框架时更改它的背景颜色,并在单击另一个框架时恢复它的颜色。
页面.xaml
where:
statusCode | expectedResult
200 | 2
400 | 3
404 | "ERROR"
Pageviewmodel.cs
<ScrollVIEw OrIEntation="Horizontal" GrID.Row="1" HorizontalScrollbarVisibility="Never" BACkgroundcolor="transparent">
<StackLayout OrIEntation="Horizontal"
margin="15,8,0">
<StackLayout OrIEntation="Horizontal" BindableLayout.Itemssource="{Binding CategorIEs}">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="model:categoryDTO">
<Frame BACkgroundcolor="{Binding source={relativesource AncestorType={x:Type vm:Pageviewmodel}},Path=Framecolor}"
CornerRadius="15"
VerticalOptions="Center"
HasShadow="false"
padding="15,8">
<Label Text="{Binding namE}"
FontSize="Micro"
HorizontalOptions="Center"
VerticalOptions="Center"
Textcolor="White"
FontAttributes="Bold" />
<Frame.GestureRecognizers>
<TapGestureRecognizer
numberOfTapsrequired="1"
Command="{Binding source={relativesource AncestorType={x:Type vm:Pageviewmodel}},Path=categoryTappeD}"
CommandParameter="{Binding .}">
</TapGestureRecognizer>
</Frame.GestureRecognizers>
</Frame>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
非常感谢您的帮助。
您的类别 DTO 应该实现 INotifyPropertyChanged 并在其中包含一个 IsSELEcted 公共属性。
<Frame.triggers>
<Datatrigger TargetType="Frame"
Binding="{Binding IsSELEcteD}"
Value="True">
<Setter Property ="BACkgroundColor" Value ="Yellow"/>
</Datatrigger>
<Datatrigger TargetType="Frame"
Binding="{Binding IsSELEcteD}"
Value="false">
<Setter Property ="BACkgroundColor" Value ="White"/>
</Datatrigger>
</Frame.triggers>
,
我使用prism MVVM 创建了一个示例应用程序,您可以将其克隆并在您的机器上本地运行here
,您可以在 Frame 上添加焦点事件,例如:
<Frame x:Name="frame1"
BACkgroundColor="Blue"
Focused="onfocus">
</Frame>
<Frame x:Name="frame2"
BACkgroundColor="Blue"
Focused="onfocus">
背后的代码:
public void onfocus(object obj,EventArgs E)
{
var myframe = obj as Frame;
myframe.BACkgroundColor = Color.Red;
}
以上是大佬教程为你收集整理的使用 MVVM 在 Xamarin.Forms 中单击时如何更改 Frame 控件的颜色全部内容,希望文章能够帮你解决使用 MVVM 在 Xamarin.Forms 中单击时如何更改 Frame 控件的颜色所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。