Xamarin.iOS のドキュメント
日付コンテンツのカスタマイズ
コントロール > Calendar > 機能 > 日付コンテンツのカスタマイズ

C1Calendar コントロールを使用して、日付スロットにカスタムコンテンツを追加できます。それには、DaySlotLoading イベントをサブスクライブし、これらの日付スロットの背景に画像などのカスタムコンテンツを適用します。この機能を使用して、カレンダーに天気関連の情報を表示できます。

次の図は、日付スロットにカスタムコンテンツを追加した後の C1Calendar です。このカレンダーには、さまざまなアイコンで天気関連の情報が表示されます。

次のコード例は、C# で C1Calendar コントロールの日付スロットにカスタムコンテンツを追加する方法を示します。この例では、「クイックスタート」で作成したサンプルを使用します。

  1. ViewController.cs ファイルの内容を次のコードに置き換えます。
    Objective-C
    コードのコピー
    using Foundation;
    using System;
    using UIKit;
    using C1.iOS.Calendar;
    
    
    namespace C1Calendar101
    {
        public partial class CustomDayController : UIViewController
        {
            public CustomDayController(IntPtr handle) : base(handle)
            {
            }
    
            public override void LoadView()
            {
                base.LoadView();
                Calendar.DaySlotLoading += OnDaySlotLoading;
                Calendar.Refresh();
            }
    
            public override void WillAnimateRotation(UIInterfaceOrientation toInterfaceOrientation, double duration)
            {
                base.WillAnimateRotation(toInterfaceOrientation, duration);
                Calendar.Refresh();
            }
    
            private void OnDaySlotLoading(object sender, CalendarDaySlotLoadingEventArgs e)
            {
                // 今月の特定日の天気画像を追加する
                var today = DateTime.Today;
                if (e.Date.Year == today.Year &&
                    e.Date.Month == today.Month &&
                    e.Date.Day >= 14 && e.Date.Day <= 23)
                {
                    var iv = new UIImageView();
                    var tv = new UILabel();
                    var slot = new UIStackView(new UIView[] { tv, iv });
                    slot.Axis = InterfaceOrientation == UIInterfaceOrientation.Portrait || InterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown ? UILayoutConstraintAxis.Vertical : UILayoutConstraintAxis.Horizontal;
    
                    tv.Text = e.Date.Day.ToString();
    
                    UIImage image;
                    switch (e.Date.Day % 5)
                    {
                        default:
                        case 0:
                            image = UIImage.FromFile(@"Images/Cloudy.png");
                            break;
                        case 1:
                            image = UIImage.FromFile(@"Images/PartlyCloudy.png");
                            break;
                        case 2:
                            image = UIImage.FromFile(@"Images/Rain.png");
                            break;
                        case 3:
                            image = UIImage.FromFile(@"Images/Storm.png");
                            break;
                        case 4:
                            image = UIImage.FromFile(@"Images/Sun.png");
                            break;
    
                    }
                    image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
                    iv.UserInteractionEnabled = true;
                    iv.TintColor = UIColor.Black;
                    iv.ContentMode = UIViewContentMode.ScaleAspectFit;
                    iv.Image = image;
                    e.DaySlot = slot;
                }
            }
        }
    }