|
NextCommand = new Command(
execute: () =>
{
ViewModel.ModelCollecion[ViewModel.CardIndex].IsVisible = false;
ViewModel.CardIndex += 1;
ViewModel.ModelCollecion[ViewModel.CardIndex].IsVisible = true;
});
PreviousCommand = new Command(
execute: () =>
{
if (ViewModel.CardIndex > 0)
{
ViewModel.ModelCollecion[ViewModel.CardIndex].IsVisible = false;
ViewModel.CardIndex -= 1;
ViewModel.ModelCollecion[ViewModel.CardIndex].IsVisible = true;
}
}); |
|
<Grid HeightRequest="500" WidthRequest="300" x:Name="cardLayout"
VerticalOptions="Center"
HorizontalOptions="Center" BackgroundColor="#F0F0F0">
<cards:SfCardView BindingContext="{Binding CurrentCard}" BackgroundColor="{Binding Color}" Margin="10">
<Grid>
<Label Text="This is CardView" HorizontalOptions="CenterAndExpand"
VerticalTextAlignment="Center"/>
<Button Margin="10" Text="Previous" Command="{Binding PreviousCommand}"
IsVisible="{Binding PreviousCommandVisible}"
HorizontalOptions="StartAndExpand" VerticalOptions="EndAndExpand"/>
<Button Margin="10" Text="Next" Command="{Binding NextCommand}"
IsVisible="{Binding NextCommandVisible}"
HorizontalOptions="EndAndExpand" VerticalOptions="EndAndExpand"/>
</Grid>
</cards:SfCardView>
</Grid> |
|
public Model()
{
NextCommand = new Command(
execute: () =>
{
ViewModel.Index += 1;
ViewModel.CurrentCard = ViewModel.ModelCollecion[ViewModel.Index];
});
PreviousCommand = new Command(
execute: () =>
{
ViewModel.Index -= 1;
ViewModel.CurrentCard = ViewModel.ModelCollecion[ViewModel.Index];
});
}
|
|
public class ViewModel :INotifyPropertyChanged
{
private Model currentCard;
public Model CurrentCard
{
get { return currentCard; }
set
{
currentCard = value;
OnPropertyChanged("CurrentCard");
}
}
public ObservableCollection<Model> ModelCollecion { get; set; }
public int Index { get; set; }
public ViewModel()
{
ModelCollecion = new ObservableCollection<Model>();
ModelCollecion.Add(new Model() { Color = Color.Cyan, PreviousCommandVisible=false });
ModelCollecion.Add(new Model() { Color = Color.Yellow});
ModelCollecion.Add(new Model() { Color = Color.Red});
ModelCollecion.Add(new Model() { Color = Color.Orange, NextCommandVisible=false });
Index = 0;
CurrentCard = ModelCollecion[Index];
foreach (var item in ModelCollecion)
{
item.ViewModel = this;
}
} |