Net maui How to add sliding animation to Carousel with timer ?

Sami 261 Reputation points
2023-05-17T21:54:14.68+00:00

Net maui How to add sliding animation to Carousel with timer ?

Platform android and ios

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,414 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 47,671 Reputation points Microsoft Vendor
    2023-05-18T02:36:56.9866667+00:00

    Hello,

    protected override async void OnAppearing() is not accepted C# side and position also is not working that missing something?

    If you add <CarouselView> in the ContentView, you can add CreateTimer to the ContentView's background code. You can change the current items by mainCarouselView.Position, then get the itemsource's count like following code

    public partial class MyView : ContentView
    {
       public static readonly BindableProperty ItemsSourceProperty =
                  BindableProperty.Create(nameof(ItemsSource), typeof(ObservableCollection<string>), typeof(MyView));
    
       public ObservableCollection<string> ItemsSource
        {
            get => (ObservableCollection<string>)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value);
        }
        public MyView()
        {
            InitializeComponent();
            var timer = Application.Current.Dispatcher.CreateTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += (s, e) =>
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    mainCarouselView.Position = (mainCarouselView.Position + 1) % ItemsSource.Count;
               });
            };
            timer.Start();
        }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful