Xamarin.forms 使用绑定隐藏和显示其他字段,使用Xamarin隐藏和显示MVVM 我试图在我的页面中有这样的事情:
关键是在第一个选项中切换显示内容,在第二个选项中切换显示其他内容 但我的代码不起作用,看起来像这样,什么也不做: 这是我的Xaml:Xamarin.forms 使用绑定隐藏和显示其他字段,使用Xamarin隐藏和显示MVVM 我试图在我的页面中有这样的事情:,xamarin.forms,mvvm,Xamarin.forms,Mvvm,关键是在第一个选项中切换显示内容,在第二个选项中切换显示其他内容 但我的代码不起作用,看起来像这样,什么也不做: 这是我的Xaml: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:components="clr-names
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:components="clr-namespace:OneTiendita.Components"
x:Class="OneTiendita.Pages.StoreDetailPage">
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout>
<Image Source="https://mujerejecutiva.com.mx/wp-content/uploads/2020/04/tiendita_de_la_esquina-scaled.jpg" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" HeightRequest="250" Aspect="AspectFill"/>
<Label Text="Tiendita" FontSize="25" FontAttributes="Bold" Padding="20"/>
<Label Text="Esta tiendita la encontre en una esquina, parece que no tiene muchos productos." FontSize="18" Padding="20,0,20,20"/>
<StackLayout Orientation="Horizontal" Padding="10,0,20,20">
<Label Text="Info" FontSize="18" FontAttributes="Bold" Padding="20,0,10,20">
<Label.GestureRecognizers
Command="{Binding InfoCommand}" />
</Label>
<Label Text="Opinions" FontSize="18" FontAttributes="Bold" Padding="10,0,20,20">
<Label.GestureRecognizers
Command="{Binding OpinionCommand}" />
</Label>
</StackLayout>
<components:StoreInfo IsVisible="{Binding infoVisible}"/>
<components:StoreOpinions IsVisible="{Binding opinionsVisible}"/>
</StackLayout>
<Image Source="https://cdn.onlinewebfonts.com/svg/img_521981.png"
HeightRequest="60"
WidthRequest="60"
AbsoluteLayout.LayoutBounds="0.98,0.98,-1,-1"
AbsoluteLayout.LayoutFlags="PositionProportional"/>
</AbsoluteLayout>
</ContentPage>
欢迎提供任何帮助共享代码有两个问题 第一个是用于
标签
(测试背景色)的手势识别器,需要修改如下:
<Label Text="Info"
FontSize="18"
FontAttributes="Bold"
BackgroundColor="LightCoral"
Padding="20,0,10,20">
<Label.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1"
Command="{Binding InfoCommand}" />
</Label.GestureRecognizers>
</Label>
<Label Text="Opinions"
FontSize="18"
BackgroundColor="Beige"
FontAttributes="Bold"
Padding="10,0,20,20">
<Label.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1"
Command="{Binding OpinionCommand}" />
</Label.GestureRecognizers>
</Label>
public class StoreDetailViewModel: INotifyPropertyChanged
{
private bool infovisible = true;
public bool infoVisible
{
set
{
if (infovisible != value)
{
infovisible = value;
OnPropertyChanged("infoVisible");
}
}
get
{
return infovisible;
}
}
private bool opinionvisible = false;
public bool opinionVisible
{
set
{
if (opinionvisible != value)
{
opinionvisible = value;
OnPropertyChanged("opinionVisible");
}
}
get
{
return opinionvisible;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public ICommand InfoCommand { get; set; }
public ICommand OpinionCommand { get; set; }
public StoreDetailViewModel()
{
InfoCommand = new Command(InfoStore);
OpinionCommand = new Command(OpinionStore);
}
private void InfoStore(object obj)
{
infovisible = true;
OnPropertyChanged("infoVisible");
opinionvisible = false;
OnPropertyChanged("opinionVisible");
}
private void OpinionStore(object obj)
{
infovisible = false;
OnPropertyChanged("infoVisible");
opinionvisible = true;
OnPropertyChanged("opinionVisible");
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
其效果是:
我完全按照你说的做了,但仍然不起作用:(@FeberCastellon您可以在
InfoStore
和OpinionStore
方法中添加断点,以检查是否首先调用了Tap方法。它最终成功了,只是我犯了一个愚蠢的错误,忘了在ViewModel中初始化绑定:真丢脸。之后,您的解决方案工作得很好,非常感谢
public class StoreDetailViewModel: INotifyPropertyChanged
{
private bool infovisible = true;
public bool infoVisible
{
set
{
if (infovisible != value)
{
infovisible = value;
OnPropertyChanged("infoVisible");
}
}
get
{
return infovisible;
}
}
private bool opinionvisible = false;
public bool opinionVisible
{
set
{
if (opinionvisible != value)
{
opinionvisible = value;
OnPropertyChanged("opinionVisible");
}
}
get
{
return opinionvisible;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public ICommand InfoCommand { get; set; }
public ICommand OpinionCommand { get; set; }
public StoreDetailViewModel()
{
InfoCommand = new Command(InfoStore);
OpinionCommand = new Command(OpinionStore);
}
private void InfoStore(object obj)
{
infovisible = true;
OnPropertyChanged("infoVisible");
opinionvisible = false;
OnPropertyChanged("opinionVisible");
}
private void OpinionStore(object obj)
{
infovisible = false;
OnPropertyChanged("infoVisible");
opinionvisible = true;
OnPropertyChanged("opinionVisible");
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}