Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Myoga1012/f75bc04a615b97def9dd to your computer and use it in GitHub Desktop.
Save Myoga1012/f75bc04a615b97def9dd to your computer and use it in GitHub Desktop.
Windowsストアアプリ(WinRT)上でカレンダーを表示するソースコードです(言語はXAMLとC#です)。
// 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka)
// Twitter : https://twitter.com/Myoga1012
using System;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace WSAPP {
// カレンダー用オブジェクトです。
struct DayObj {
public string DayString { get; set; } // 日付
public SolidColorBrush DayTextColor { get; set; } // 文字色
public SolidColorBrush DayBgColor { get; set; } // 背景色
}
class WeekObj { public DayObj[] Days { get; set; } } // 週単位のDayObjの配列
// カレンダーオブジェクトの本体です。
class Calendar {
public WeekObj[] Weeks{ get; set; }
public string NowMonth { get; set; }
public Calendar() {
Weeks = new WeekObj[6];
SolidColorBrush weekdaytc = new SolidColorBrush( Colors.Azure ); // 平日の文字色と背景色です。
SolidColorBrush weekdaybc = new SolidColorBrush( Colors.Black );
SolidColorBrush sattc = new SolidColorBrush( Colors.DodgerBlue ); // 土曜日の文字色と背景色です。
SolidColorBrush satbc = new SolidColorBrush( Color.FromArgb( 0xFF, 0x00, 0x14, 0x2F ) );
SolidColorBrush suntc = new SolidColorBrush( Colors.Magenta ); // 日曜日の文字色と背景色です。
SolidColorBrush sunbc = new SolidColorBrush( Color.FromArgb( 0xFF, 0x20, 0x00, 0x20 ) );
// 初期化します。
for( int i = 0; i < Weeks.Length; i++ ) {
Weeks[i] = new WeekObj();
Weeks[i].Days = new DayObj[7];
for( int j = 0; j < Weeks[i].Days.Length; j++ ) {
Weeks[i].Days[j].DayTextColor = ( j == 0 ? suntc : ( j == 6 ? sattc : weekdaytc ) );
Weeks[i].Days[j].DayBgColor = ( j == 0 ? sunbc : ( j == 6 ? satbc : weekdaybc ) );
}
}
// カレンダーの日付データを構成します。
DateTime now1 = new DateTime( DateTime.Today.Year, DateTime.Today.Month, 1 );
int last = DateTime.DaysInMonth( now1.Year, now1.Month );
NowMonth = string.Format( "カレンダー - {0}年{1}月", now1.Year, now1.Month ); // タイトルを設定します。
for( int seq = ( int )now1.DayOfWeek, day = 1; day <= last; seq++, day++ ) {
Weeks[seq / 7].Days[seq % 7].DayString = day.ToString();
}
}
}
public sealed partial class MainPage : Page {
public MainPage() { this.InitializeComponent(); }
}
}
// Calender.cs( For Windowsストアアプリ )
// Copyright (c) 2014 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
<!--
名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka)
Twitter : https://twitter.com/Myoga1012
-->
<Page x:Class="WSAPP.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WSAPP">
<Page.DataContext>
<!-- カレンダーのインスタンスです。 -->
<local:Calendar x:Name="calendar"/>
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="30">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- タイトル部( YYYY年MM月 ) -->
<TextBlock Grid.Row="0" FontSize="50" VerticalAlignment="Center" Text="{Binding NowMonth}"/>
<!-- カレンダー部 -->
<!-- カレンダーの週を構成します。 -->
<ItemsControl Grid.Row="1" ItemsSource="{Binding Weeks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- カレンダーの各週の日付を構成します。 -->
<ItemsControl ItemsSource="{Binding Days}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- カレンダーの日付を出力します。 -->
<Border BorderBrush="White" BorderThickness="1"
Background="{Binding DayBgColor}"
Width="150" Height="100">
<TextBlock Text="{Binding DayString}" Foreground="{Binding DayTextColor}"
FontSize="50" TextAlignment="Left" VerticalAlignment="Top" Margin="5"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Grid>
</Page>
<!--
Calender.xaml( For Windowsストアアプリ )
Copyright (c) 2014 Myoga-TN All Rights Reserved.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment