简体   繁体   中英

How to add school events to XamForms.Calendar.Control calendar?

I would like to add school lessons, exams, etc to specific dates dynamically to my calendar (for example: right click the date to add the lesson/exam or select the date and having an add event button). Tried many ways for days but nothing were successful. These are my C# codes:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamForms.Controls;

namespace ManageUni.Views.Navigation
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TimeTableView : ContentPage
    {
        public TimeTableView()
        {
            XamForms.Controls.Calendar calendar1 = new XamForms.Controls.Calendar
            {
                BorderColor = Color.Gray,
                BorderWidth = 3,
                BackgroundColor = Color.Gray,
                StartDay = DayOfWeek.Sunday,
                StartDate = DateTime.Now
            };



            InitializeComponent();
        }



<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"   
             xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar" 
             x:Class="ManageUni.Views.Navigation.TimeTableView">
    <ContentPage.Content>



        <StackLayout>
            <Label Text="Órarend"
               VerticalOptions="CenterAndExpand"
               HorizontalOptions="CenterAndExpand" />
            <controls:Calendar Padding="10,0,10,0"   
                           SelectedBorderWidth="4"   
                           DisabledBorderColor="Black"  
                           ShowNumberOfWeek="false"  
                           StartDay="Monday"  
                           TitleLabelTextColor="Purple"  
                           TitleLeftArrowTextColor="Blue"  
                           SelectedDate="{Binding Date}"  
                           SpecialDates="{Binding Attendances}"  
                           DateCommand="{Binding DateChosen}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage> 


You could use DateClicked event to simulate the add event. And then display a prompt to get the event to want to input. After that add the event into special datas. The last thing is to refresh.

Xaml:

 <Grid Margin="16,44,16,16">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label
            HorizontalOptions="CenterAndExpand"
            Text="Órarend"
            VerticalOptions="CenterAndExpand" />
        <controls:Calendar
            x:Name="calendar"
            Grid.Row="1"
            Padding="10,0,10,0"
            DateClicked="Calendar_DateClicked"
            DateCommand="{Binding DateChosen}"
            DisabledBorderColor="Black"
            SelectedBorderWidth="4"
            SelectedDate="{Binding Date}"
            ShowNumberOfWeek="false"
            SpecialDates="{Binding Attendances}"
            StartDay="Monday"
            TitleLabelTextColor="Purple"
            TitleLeftArrowTextColor="Blue" />
    </Grid>

Code behind:

  async void Calendar_DateClicked(object sender, XamForms.Controls.DateTimeEventArgs e)
    {
        string result = await DisplayPromptAsync("SpecialEvent", "Create a new Event:");

        if (result == null)
        {
            return;
        }
        var specialDates = calendar.SpecialDates;

        SpecialDate newDate = new SpecialDate(e.DateTime)
        {
            Selectable = true,
            BackgroundPattern = new BackgroundPattern(1)
            {
                Pattern = new List<Pattern>
                     {
                         new Pattern { WidthPercent = 1f, HightPercent = 0.6f, Color = Color.Transparent },
                         new Pattern{ WidthPercent = 1f, HightPercent = 0.4f, Color = Color.Transparent, Text = result, TextColor=Color.Black, TextSize=11, TextAlign=TextAlign.Middle},
                     }
            }
        };
        specialDates.Add(newDate);

        calendar.SpecialDates = specialDates;
        calendar.ForceRedraw();

    }

The model and view model you could get from the GitHub. https://github.com/lubiepomaranczki/XamForms.Controls.Calendar

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM