|
| 1 | +--- |
| 2 | +title: "Carousal View in maui" |
| 3 | +author: "PrashantUnity" |
| 4 | +weight: 106 |
| 5 | +date: 2025-06-23 |
| 6 | +lastmod: 2024-06-23 |
| 7 | +dateString: July 2025 |
| 8 | +description: "Code snippet for maui auto change carousal view component" |
| 9 | +#canonicalURL: "https://canonical.url/to/page" |
| 10 | +cover: |
| 11 | + image: "cover.jpg" # image path/url |
| 12 | + alt: "Download Logo" # alt text |
| 13 | + #caption: "Optical Character Recognition" display caption under cover |
| 14 | + |
| 15 | +tags: [ "NET", "codefrydev", "C sharp", "CFD","maui" , "layout","Carousal"] |
| 16 | +keywords: [ "NET", "codefrydev", "C sharp", "CFD","maui" , "Carousal"] |
| 17 | +--- |
| 18 | + |
| 19 | + |
| 20 | +### \#\# 1. Create the Reusable Component Files 🧱 |
| 21 | + |
| 22 | +First, add a new item to your project. Choose the **.NET MAUI ContentView (XAML)** template. Name it something descriptive, like `FeaturedCarouselView.xaml`. |
| 23 | + |
| 24 | +### \#\# 2. UI code sample of the ContentView |
| 25 | + |
| 26 | +**`FeaturedCarouselView.xaml`:** |
| 27 | + |
| 28 | +```xml |
| 29 | +<?xml version="1.0" encoding="utf-8" ?> |
| 30 | +<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 31 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 32 | + xmlns:local="clr-namespace:YourMauiProjectName" |
| 33 | + x:Class="YourMauiProjectName.FeaturedCarouselView"> |
| 34 | + |
| 35 | + <VerticalStackLayout Spacing="16" BackgroundColor="{StaticResource CardBackgroundColor}"> |
| 36 | + <CarouselView x:Name="FeaturedCarousel" |
| 37 | + IndicatorView="FeaturedIndicator" |
| 38 | + HeightRequest="500"> |
| 39 | + <CarouselView.ItemTemplate> |
| 40 | + <DataTemplate x:DataType="local:CarouselItem"> |
| 41 | + <VerticalStackLayout Padding="16,0" Spacing="24"> |
| 42 | + <Border Style="{StaticResource CardBorderStyle}" StrokeShape="RoundRectangle 12" HeightRequest="300"> |
| 43 | + <Image Source="{Binding ImageUrl}" Aspect="AspectFill" /> |
| 44 | + </Border> |
| 45 | + <VerticalStackLayout Spacing="16" HorizontalOptions="Center" Padding="16,0"> |
| 46 | + <Label Text="{Binding Title}" TextColor="{StaticResource ForegroundColor}" FontSize="24" FontAttributes="Bold" HorizontalTextAlignment="Center" /> |
| 47 | + <Label Text="{Binding Description}" TextColor="{StaticResource MutedForegroundColor}" FontSize="16" HorizontalTextAlignment="Center" /> |
| 48 | + <Button Text="Discover More →" Style="{StaticResource PrimaryButtonStyle}" HorizontalOptions="Center" /> |
| 49 | + </VerticalStackLayout> |
| 50 | + </VerticalStackLayout> |
| 51 | + </DataTemplate> |
| 52 | + </CarouselView.ItemTemplate> |
| 53 | + </CarouselView> |
| 54 | + |
| 55 | + <IndicatorView x:Name="FeaturedIndicator" |
| 56 | + IndicatorColor="{StaticResource BorderColor}" |
| 57 | + SelectedIndicatorColor="{StaticResource PrimaryColor}" |
| 58 | + HorizontalOptions="Center" |
| 59 | + Margin="0,0,0,16"/> |
| 60 | + </VerticalStackLayout> |
| 61 | + |
| 62 | +</ContentView> |
| 63 | +``` |
| 64 | + |
| 65 | +### \#\# 3. Logic to the Component's Code-Behind |
| 66 | + |
| 67 | +**Important:** We use the `Loaded` and `Unloaded` events to start and stop the timer. |
| 68 | + |
| 69 | +**`FeaturedCarouselView.xaml.cs`:** |
| 70 | + |
| 71 | +```csharp |
| 72 | +// Define the data record here or in a separate file |
| 73 | +public record CarouselItem(string Title, string Description, string ImageUrl); |
| 74 | + |
| 75 | +public partial class FeaturedCarouselView : ContentView |
| 76 | +{ |
| 77 | + private IDispatcherTimer _timer; |
| 78 | + private readonly List<CarouselItem> _carouselItems; |
| 79 | + |
| 80 | + public FeaturedCarouselView() |
| 81 | + { |
| 82 | + InitializeComponent(); |
| 83 | + |
| 84 | + _carouselItems = new List<CarouselItem> |
| 85 | + { |
| 86 | + new CarouselItem("Solitaire Diamond Ring", "An exquisite solitaire diamond ring, perfect for engagements.", "https://picsum.photos/600/600?random=1"), |
| 87 | + new CarouselItem("Ruby Heart Pendant", "A stunning heart-shaped ruby pendant on a delicate rose gold chain.", "https://picsum.photos/600/600?random=2"), |
| 88 | + new CarouselItem("Emerald Tennis Bracelet", "A timeless classic featuring brilliant-cut emeralds.", "https://picsum.photos/600/600?random=3") |
| 89 | + }; |
| 90 | + |
| 91 | + FeaturedCarousel.ItemsSource = _carouselItems; |
| 92 | + |
| 93 | + // Use the Loaded and Unloaded events to manage the timer |
| 94 | + this.Loaded += OnLoaded; |
| 95 | + this.Unloaded += OnUnloaded; |
| 96 | + } |
| 97 | + |
| 98 | + private void OnLoaded(object sender, EventArgs e) |
| 99 | + { |
| 100 | + _timer = Application.Current.Dispatcher.CreateTimer(); |
| 101 | + _timer.Interval = TimeSpan.FromSeconds(5); |
| 102 | + _timer.Tick += Timer_Tick; |
| 103 | + _timer.Start(); |
| 104 | + } |
| 105 | + |
| 106 | + private void OnUnloaded(object sender, EventArgs e) |
| 107 | + { |
| 108 | + _timer.Stop(); |
| 109 | + _timer.Tick -= Timer_Tick; |
| 110 | + _timer = null; |
| 111 | + } |
| 112 | + |
| 113 | + private void Timer_Tick(object sender, EventArgs e) |
| 114 | + { |
| 115 | + int nextPosition = (FeaturedCarousel.Position + 1) % _carouselItems.Count; |
| 116 | + FeaturedCarousel.Position = nextPosition; |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### \#\# 4. Use Your New Component |
| 122 | + |
| 123 | +**Your main page's XAML (e.g., `HomePage.xaml`):** |
| 124 | + |
| 125 | +```xml |
| 126 | +<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 127 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 128 | + xmlns:local="clr-namespace:YourMauiProjectName" |
| 129 | + x:Class="YourMauiProjectName.HomePage" |
| 130 | + Title="Home"> |
| 131 | + |
| 132 | + <ScrollView> |
| 133 | + <VerticalStackLayout> |
| 134 | + |
| 135 | + <local:FeaturedCarouselView /> |
| 136 | + |
| 137 | + </VerticalStackLayout> |
| 138 | + </ScrollView> |
| 139 | + |
| 140 | +</ContentPage> |
| 141 | +``` |
0 commit comments