How to design the week view calender in xamarin forms?

PoojaMorePoojaMore Member ✭✭
edited May 2018 in Xamarin.Forms

I am confusing, which page i need to use for that carousel page or any other page in xamarin froms

Best Answer

  • PoojaMorePoojaMore ✭✭
    edited June 2018 Accepted Answer

    @ Fotal Hi, my requirement same as yours. iNeed to create the Calender in Xamarin forms with Week, Days,...etc. can mi send your's code

    Hi @Fotal....
    at time of posting this i also want to design same...
    and i am design with little difference.... i used Grid.

    in XAML
    <?xml version="1.0" encoding="utf-8" ?>

    <ContentPage.Content>

    <Grid.RowDefinitions>



    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>

    </Grid.ColumnDefinitions>

            <Grid Grid.Row="0" Grid.Column="0" x:Name="grdHeader" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0" BackgroundColor="#0070FF">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
    
                <Label Grid.Row="0" Grid.Column="0" x:Name="lblMonth" Text="Month" TextColor="White" HorizontalOptions="End" VerticalOptions="Center"/>
                <Label Grid.Row="0" Grid.Column="1" x:Name="lblYear" Text="Year" TextColor="White" HorizontalOptions="Start" VerticalOptions="Center"/>
    
            </Grid>
    
            <Grid Grid.Row="1" Grid.Column="0" x:Name="grdDayOfWeek" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1" />
                    <RowDefinition Height="Auto" />
                    <!--<RowDefinition Height="1" />-->
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="1" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                </Grid.ColumnDefinitions>
    
                <!--Lines For Row 1-->
                <BoxView Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Color="Gray" WidthRequest="1" HeightRequest="25" />
                <BoxView Grid.Column="16" Grid.Row="1" Grid.RowSpan="2" Color="Gray" WidthRequest="1" HeightRequest="25"/>
    
                <!--Lines For Row 0-->                
                <BoxView Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="3" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="7" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="9" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="11" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="13" Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="15" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
    
                <!--Lines For Row 1-->
                <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Text="" Style="{StaticResource lblWeekDays}" />
                <Label Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" Text="SUN" Style="{StaticResource lblWeekDays}" />
                <Label Grid.Row="1" Grid.Column="5" Grid.ColumnSpan="2" Text="MON" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="7" Grid.ColumnSpan="2" Text="TUE" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="9" Grid.ColumnSpan="2" Text="WED" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="11" Grid.ColumnSpan="2" Text="THU" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="13" Grid.ColumnSpan="2" Text="FRI" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="15" Grid.ColumnSpan="2" Text="SAT" Style="{StaticResource lblWeekDays}" />
    
    
                <!--Lines For Row 3-->
                <BoxView Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="3" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="5" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="7" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="9" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="11" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="13" Grid.Row="3" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="15" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
    
    
            </Grid>
    
            <ScrollView x:Name="ScrollWeek" Orientation="Vertical" Grid.Row="2" Grid.Column="0" IsClippedToBounds="True" >
                <Grid x:Name="grdWeekDates" Padding="0" Margin="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0">
    
            </Grid>
            </ScrollView>
        </Grid>
    </ContentPage.Content>
    

    in xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;

    namespace SampleCalender2
    {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DemoWeekDesign : ContentPage
    {
    DateTime TodayDate = DateTime.Now.Date;
    Label lblWeekDate;
    Label lblTime;
    Frame frmColor;
    Button btnClick;
    string txtTimeAM = "AM";
    string txtTimePM = "PM";
    int iTime = 1;
    public DemoWeekDesign(DateTime dateNow)
    {
    InitializeComponent();

            CreateWeek(dateNow);
        }
    
        private void CreateWeek(DateTime dateNow)
        {
            int iDay = (int)dateNow.Day;
            int iMonth = (int)dateNow.Month;
            int iYear = (int)dateNow.Year;
            int iStartDayWeek = (int)new DateTime(iYear, iMonth, iDay).DayOfWeek;
            //DateTime FirstDate = new DateTime(dateNow.Year, dateNow.Month, 1);
            //int iDate = 1;
            int daysInMonth = 0;
            daysInMonth = DateTime.DaysInMonth(iYear, iMonth);
            //DateTime EndDate = new DateTime(dateNow.Year, dateNow.Month, daysInMonth);
    
            if (dateNow.Month == 2)
            {
                if (DateTime.IsLeapYear(dateNow.Year))
                {
                    daysInMonth = 29;
                }
            }
    
            int iDays = iDay;
            if (iDay > iStartDayWeek)
            {
                //Months = iMonth;
                iDays = iDay - iStartDayWeek;
            }
            else
            {
                if (iDays < 8)    //if Date bet 1 to 6 and the Last Months dates
                {
                    iDays = 1;
    
                }
            }
    
    
            //Grid Week of Dates binding
            for (int r = 2; r == 2; r++)
            {
                for (int c = 3; c < grdDayOfWeek.ColumnDefinitions.Count; c++)
                {
                    if (r == 2)
                    {
                        if (iDays <= daysInMonth && iDays < 32)
                        {
                            if (c % 2 != 0)
                            {
                                iStartDayWeek = (int)new DateTime(iYear, iMonth, iDays).DayOfWeek;
                                if (c == iStartDayWeek * 2 + 3)
                                {
                                    lblWeekDate = new Label() { Text = iDays.ToString(), TextColor = Color.Gray, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Start };
                                    grdDayOfWeek.Children.Add(lblWeekDate, c, r);
    
                                    if ((iStartDayWeek == 0 || iStartDayWeek == 1 || iStartDayWeek == 2 || iStartDayWeek == 3 || iStartDayWeek == 4 || iStartDayWeek == 5) || iDays != iStartDayWeek)
                                        iDays++;
                                    else
                                        iDays--;
                                }
                            }
                        }
                    }
                }
            }
    
            grdWeekDates = new Grid();
            grdWeekDates.IsClippedToBounds = true;
            grdWeekDates.RowSpacing = 0;
            grdWeekDates.ColumnSpacing = 0;
            grdWeekDates.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Absolute) });
    
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
    
            for (int rowIndex = 1; rowIndex < 49; rowIndex++)
            {
                if (rowIndex % 2 == 0)
                {
                    grdWeekDates.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Absolute) });
                }
                else
                {
                    grdWeekDates.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                }
    
                for (int columnIndex = 0; columnIndex < 17; columnIndex++)
                {
                    BoxView VerticalHoriBV;
                    if (columnIndex % 2 == 0)
                    {
                        VerticalHoriBV = new BoxView() { Color = Color.Gray, WidthRequest = 1, VerticalOptions = LayoutOptions.FillAndExpand };
                        grdWeekDates.Children.Add(VerticalHoriBV, columnIndex, rowIndex);
                        if (columnIndex == 0 && rowIndex % 2 == 0 && rowIndex != 48)
                        {
                            Grid.SetRowSpan(VerticalHoriBV, 2);
                        }
                    }
    
                    if (columnIndex % 2 != 0 && rowIndex % 2 == 0)
                    {
                        if (columnIndex == 1 && rowIndex % 2 == 0 && rowIndex != 48)
                        {
                            //Print Time
                            if (rowIndex <= 22)
                            {
                                lblTime = new Label() { Text = iTime.ToString() + " " + txtTimeAM, TextColor = Color.Gray, FontSize = 15, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start,Margin=new Thickness(0) };
                                grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                Grid.SetRowSpan(lblTime, 2);
                                iTime++;
                            }
                            if (rowIndex > 23 && rowIndex < 49)
                            {
                                if (iTime != 0)
                                {
                                    if (iTime == 12 && rowIndex == 25)
                                    {
                                        lblTime = new Label() { Text = iTime.ToString() + " " + txtTimePM, TextColor = Color.Gray, FontSize = 15,HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start };
                                        grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                        Grid.SetRowSpan(lblTime, 2);
                                    }
                                    else
                                    {
                                        lblTime = new Label() { Text = iTime.ToString() + " " + txtTimePM, TextColor = Color.Gray, FontSize = 15,  HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start, Margin = new Thickness(0) };
                                        grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                        Grid.SetRowSpan(lblTime, 2);
                                    }
    
                                    if (iTime == 12)
                                        iTime = 1;
                                    else
                                        iTime++;
                                }
                            }
                        }
                        else
                        {
                            VerticalHoriBV = new BoxView() { Color = Color.Gray, HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = 1 };
                            grdWeekDates.Children.Add(VerticalHoriBV, columnIndex, rowIndex);
                        }
                    }
    
                    if (columnIndex % 2 != 0 && rowIndex % 2 != 0 && columnIndex != 1)
                    {
                        btnClick = new Button() { Text ="", BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Margin = new Thickness(0) };
                        grdWeekDates.Children.Add(btnClick, columnIndex, rowIndex);
                    }
    
                }
    
            }
            ScrollWeek.Content = grdWeekDates;
            grdUI.Children.Add(ScrollWeek, 0, 2);
    
        }
    
    }
    

    }

Answers

  • PoojaMorePoojaMore Member ✭✭
    edited June 2018 Accepted Answer

    @ Fotal Hi, my requirement same as yours. iNeed to create the Calender in Xamarin forms with Week, Days,...etc. can mi send your's code

    Hi @Fotal....
    at time of posting this i also want to design same...
    and i am design with little difference.... i used Grid.

    in XAML
    <?xml version="1.0" encoding="utf-8" ?>

    <ContentPage.Content>

    <Grid.RowDefinitions>



    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>

    </Grid.ColumnDefinitions>

            <Grid Grid.Row="0" Grid.Column="0" x:Name="grdHeader" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0" BackgroundColor="#0070FF">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
    
                <Label Grid.Row="0" Grid.Column="0" x:Name="lblMonth" Text="Month" TextColor="White" HorizontalOptions="End" VerticalOptions="Center"/>
                <Label Grid.Row="0" Grid.Column="1" x:Name="lblYear" Text="Year" TextColor="White" HorizontalOptions="Start" VerticalOptions="Center"/>
    
            </Grid>
    
            <Grid Grid.Row="1" Grid.Column="0" x:Name="grdDayOfWeek" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1" />
                    <RowDefinition Height="Auto" />
                    <!--<RowDefinition Height="1" />-->
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="1" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="1" />
                </Grid.ColumnDefinitions>
    
                <!--Lines For Row 1-->
                <BoxView Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Color="Gray" WidthRequest="1" HeightRequest="25" />
                <BoxView Grid.Column="16" Grid.Row="1" Grid.RowSpan="2" Color="Gray" WidthRequest="1" HeightRequest="25"/>
    
                <!--Lines For Row 0-->                
                <BoxView Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="3" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="7" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="9" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="11" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="13" Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="15" Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
    
                <!--Lines For Row 1-->
                <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Text="" Style="{StaticResource lblWeekDays}" />
                <Label Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" Text="SUN" Style="{StaticResource lblWeekDays}" />
                <Label Grid.Row="1" Grid.Column="5" Grid.ColumnSpan="2" Text="MON" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="7" Grid.ColumnSpan="2" Text="TUE" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="9" Grid.ColumnSpan="2" Text="WED" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="11" Grid.ColumnSpan="2" Text="THU" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="13" Grid.ColumnSpan="2" Text="FRI" Style="{StaticResource lblBoldWeekDays}" />
                <Label Grid.Row="1" Grid.Column="15" Grid.ColumnSpan="2" Text="SAT" Style="{StaticResource lblWeekDays}" />
    
    
                <!--Lines For Row 3-->
                <BoxView Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="3" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="5" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="7" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="9" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="11" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="13" Grid.Row="3" Grid.ColumnSpan="3" Style="{StaticResource HorizontelBoxView}"/>
                <BoxView Grid.Column="15" Grid.Row="3" Grid.ColumnSpan="2" Style="{StaticResource HorizontelBoxView}"/>
    
    
            </Grid>
    
            <ScrollView x:Name="ScrollWeek" Orientation="Vertical" Grid.Row="2" Grid.Column="0" IsClippedToBounds="True" >
                <Grid x:Name="grdWeekDates" Padding="0" Margin="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0">
    
            </Grid>
            </ScrollView>
        </Grid>
    </ContentPage.Content>
    

    in xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;

    namespace SampleCalender2
    {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DemoWeekDesign : ContentPage
    {
    DateTime TodayDate = DateTime.Now.Date;
    Label lblWeekDate;
    Label lblTime;
    Frame frmColor;
    Button btnClick;
    string txtTimeAM = "AM";
    string txtTimePM = "PM";
    int iTime = 1;
    public DemoWeekDesign(DateTime dateNow)
    {
    InitializeComponent();

            CreateWeek(dateNow);
        }
    
        private void CreateWeek(DateTime dateNow)
        {
            int iDay = (int)dateNow.Day;
            int iMonth = (int)dateNow.Month;
            int iYear = (int)dateNow.Year;
            int iStartDayWeek = (int)new DateTime(iYear, iMonth, iDay).DayOfWeek;
            //DateTime FirstDate = new DateTime(dateNow.Year, dateNow.Month, 1);
            //int iDate = 1;
            int daysInMonth = 0;
            daysInMonth = DateTime.DaysInMonth(iYear, iMonth);
            //DateTime EndDate = new DateTime(dateNow.Year, dateNow.Month, daysInMonth);
    
            if (dateNow.Month == 2)
            {
                if (DateTime.IsLeapYear(dateNow.Year))
                {
                    daysInMonth = 29;
                }
            }
    
            int iDays = iDay;
            if (iDay > iStartDayWeek)
            {
                //Months = iMonth;
                iDays = iDay - iStartDayWeek;
            }
            else
            {
                if (iDays < 8)    //if Date bet 1 to 6 and the Last Months dates
                {
                    iDays = 1;
    
                }
            }
    
    
            //Grid Week of Dates binding
            for (int r = 2; r == 2; r++)
            {
                for (int c = 3; c < grdDayOfWeek.ColumnDefinitions.Count; c++)
                {
                    if (r == 2)
                    {
                        if (iDays <= daysInMonth && iDays < 32)
                        {
                            if (c % 2 != 0)
                            {
                                iStartDayWeek = (int)new DateTime(iYear, iMonth, iDays).DayOfWeek;
                                if (c == iStartDayWeek * 2 + 3)
                                {
                                    lblWeekDate = new Label() { Text = iDays.ToString(), TextColor = Color.Gray, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Start };
                                    grdDayOfWeek.Children.Add(lblWeekDate, c, r);
    
                                    if ((iStartDayWeek == 0 || iStartDayWeek == 1 || iStartDayWeek == 2 || iStartDayWeek == 3 || iStartDayWeek == 4 || iStartDayWeek == 5) || iDays != iStartDayWeek)
                                        iDays++;
                                    else
                                        iDays--;
                                }
                            }
                        }
                    }
                }
            }
    
            grdWeekDates = new Grid();
            grdWeekDates.IsClippedToBounds = true;
            grdWeekDates.RowSpacing = 0;
            grdWeekDates.ColumnSpacing = 0;
            grdWeekDates.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Absolute) });
    
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
            grdWeekDates.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Absolute) });
    
            for (int rowIndex = 1; rowIndex < 49; rowIndex++)
            {
                if (rowIndex % 2 == 0)
                {
                    grdWeekDates.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Absolute) });
                }
                else
                {
                    grdWeekDates.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                }
    
                for (int columnIndex = 0; columnIndex < 17; columnIndex++)
                {
                    BoxView VerticalHoriBV;
                    if (columnIndex % 2 == 0)
                    {
                        VerticalHoriBV = new BoxView() { Color = Color.Gray, WidthRequest = 1, VerticalOptions = LayoutOptions.FillAndExpand };
                        grdWeekDates.Children.Add(VerticalHoriBV, columnIndex, rowIndex);
                        if (columnIndex == 0 && rowIndex % 2 == 0 && rowIndex != 48)
                        {
                            Grid.SetRowSpan(VerticalHoriBV, 2);
                        }
                    }
    
                    if (columnIndex % 2 != 0 && rowIndex % 2 == 0)
                    {
                        if (columnIndex == 1 && rowIndex % 2 == 0 && rowIndex != 48)
                        {
                            //Print Time
                            if (rowIndex <= 22)
                            {
                                lblTime = new Label() { Text = iTime.ToString() + " " + txtTimeAM, TextColor = Color.Gray, FontSize = 15, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start,Margin=new Thickness(0) };
                                grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                Grid.SetRowSpan(lblTime, 2);
                                iTime++;
                            }
                            if (rowIndex > 23 && rowIndex < 49)
                            {
                                if (iTime != 0)
                                {
                                    if (iTime == 12 && rowIndex == 25)
                                    {
                                        lblTime = new Label() { Text = iTime.ToString() + " " + txtTimePM, TextColor = Color.Gray, FontSize = 15,HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start };
                                        grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                        Grid.SetRowSpan(lblTime, 2);
                                    }
                                    else
                                    {
                                        lblTime = new Label() { Text = iTime.ToString() + " " + txtTimePM, TextColor = Color.Gray, FontSize = 15,  HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start, Margin = new Thickness(0) };
                                        grdWeekDates.Children.Add(lblTime, columnIndex, rowIndex);
                                        Grid.SetRowSpan(lblTime, 2);
                                    }
    
                                    if (iTime == 12)
                                        iTime = 1;
                                    else
                                        iTime++;
                                }
                            }
                        }
                        else
                        {
                            VerticalHoriBV = new BoxView() { Color = Color.Gray, HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = 1 };
                            grdWeekDates.Children.Add(VerticalHoriBV, columnIndex, rowIndex);
                        }
                    }
    
                    if (columnIndex % 2 != 0 && rowIndex % 2 != 0 && columnIndex != 1)
                    {
                        btnClick = new Button() { Text ="", BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Margin = new Thickness(0) };
                        grdWeekDates.Children.Add(btnClick, columnIndex, rowIndex);
                    }
    
                }
    
            }
            ScrollWeek.Content = grdWeekDates;
            grdUI.Children.Add(ScrollWeek, 0, 2);
    
        }
    
    }
    

    }

  • PoojaMorePoojaMore Member ✭✭

    hi,
    i have design a calender by using CorouselView in Xamarin Forms but i don't want to hold that months on that items when i will scroll that time only that need to load and show next/previous months.
    but the carousel View creating 12 months(i given) items of item template but after 6th position of months it's not binding properly (not debugging also) and also when coming bake to previous months not binding properly

    anyone can help mi..
    Thank You.

  • Hi, i got a question. Wich static resources did you put in the xaml?

Sign In or Register to comment.