Hello,
We can do this by starting timer.Start();
after page appearing.
Firstly, we can use WeakMessager to notify page is appearing, you need install CommunityToolkit.Mvvm
nuget packages.
Then create a ViewAppearMessage
class like following code.
public class ViewAppearMessage : ValueChangedMessage<string>
{
public ViewAppearMessage(string message) : base(message)
{
}
}
Next, we can register this weak meesage in the Carousel_Loaded
method.
...
var timer = Microsoft.Maui.Controls.Application.Current.Dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += (s, e) =>
{
MainThread.BeginInvokeOnMainThread(() =>
{
totalCounter.Text = ItemsSource.Count.ToString();
currentCounter.Text = (mainCarousel.Position + 1).ToString();// default 1
mainCarousel.Position = (mainCarousel.Position + 1) % ItemsSource.Count;
});
};
WeakReferenceMessenger.Default.Register<ViewAppearMessage>(this, (r, m) =>
{
timer.Start();
});
...
In the end, if your Carousel view show in the MainPage. Send message in the MainPage's OnAppearing
method.
protected override async void OnAppearing()
{
WeakReferenceMessenger.Default.Send(new ViewAppearMessage("appearing"));
base.OnAppearing();
}
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.