如何将学校活动添加到 XamForms.Calendar.Control 日历?

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

我想将学校课程、考试等动态添加到我的日历中的特定日期(例如:右键单击日期以添加 lesson/exam 或 select 日期并设置添加事件按钮)。几天来尝试了很多方法,但没有成功。 这些是我的 C# 代码:

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> 


您可以使用 DateClicked 事件来模拟添加事件。然后显示提示以获取要输入的事件。之后将事件添加到特殊数据中。最后就是刷新了

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>

后面的代码:

  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();

    }

您可以从 GitHub 获得的模型和视图模型。 https://github.com/lubiepomaranczki/XamForms.Controls.Calendar