У меня есть свойства просмотра и привязки карусели:
<ContentView>
<controls:CarouselViewControl x:Name="CarouselData" ItemsSource="{Binding StartDisplay}"
ShowIndicators="True"
ShowArrows="False"
IndicatorsTintColor="BurlyWood" CurrentPageIndicatorTintColor="DarkGoldenrod">
<controls:CarouselViewControl.ItemTemplate>
<DataTemplate >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Image Grid.RowSpan="3" Aspect="AspectFill" Source="{Binding Image}"/>
<ContentView Grid.Row="0" Padding="60,30,60,0">
</ContentView>
<ContentView Grid.Row="1" Padding="20,10,20,50">
<Label Text="{Binding Message}"
TextColor="Black"
FontSize="20"
HorizontalTextAlignment="Center" />
</ContentView>
<ContentView Grid.Row="2" Padding="20,10,20,50">
<Button Text="Getted start" IsVisible="{Binding VisibleStartButton}"/>
</ContentView>
</Grid>
</DataTemplate>
</controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>
</ContentView>
Я указал ItemsSource для CarouselView.Class ModelView:
public class StartPageViewModel: BaseViewModel // ViewModel
{
private ObservableCollection<DataCarouselView> startDisplay;
public ObservableCollection<DataCarouselView> StartDisplay
{
get => startDisplay; set => SetProperty(ref startDisplay, value);
}
public StartPageViewModel()
{
StartDisplay = new ObservableCollection<DataCarouselView>(new[]
{
new DataCarouselView("back_1.png", "test1"),
new DataCarouselView("back_2.png", "test2"),
new DataCarouselView("back_3.png", "test3"),
new DataCarouselView("back_4.png", "test4", true)
});
OpenMenuPageCommand = new OpenMenuPageCommand(this);
}
}
class Model:
public class DataCarouselView // Model
{
public string Image { get; set; }
public string Message { get; set; }
public bool VisibleStartButton { get; set; }
public DataCarouselView(string image, string message, bool vis = false)
{
Image = image;
Message = message;
VisibleStartButton = vis;
}
}
Все работает.
Я хочу добавить команду для кнопки.Я создаю класс команды:
public class OpenMenuPageCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private StartPageViewModel viewModel;
public OpenMenuPageCommand(StartPageViewModel vm)
{
viewModel = vm;
}
public bool CanExecute(object parameter)
{
return viewModel != null;
}
public void Execute(object parameter)
{
if (CanExecute(parameter))
viewModel.OpenMenuPage();
}
}
и свойство в классе ViewModel:
public ICommand OpenMenuPageCommand { get; }
и команду метода в классе ViewModel:
public async void OpenMenuPage()
{
await navigation.PushAsync(new MenuPage());
}
Как связать команду для кнопки?Спасибо.