65.9K
CodeProject 正在变化。 阅读更多。
Home

多文化日历

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.43/5 (13投票s)

2008年4月26日

CPOL
viewsIcon

47718

downloadIcon

1468

一个支持各种日历的日历,如波斯(Farsi)、阿拉伯和英语日历,只需更改区域设置

引言

如你所知,更改文化后,所有控件都将使用新的文化。在这个控件中,你可以在一个页面上使用多个日历,并为每个日历更改文化。

重要的是,微软不支持波斯日历的UI。现在你可以非常容易地使用它。你可以从你想要的其他控件继承这个控件,并创建你自定义的控件。

Using the Code

代码非常简单,我只是重写了render方法。在渲染之前和之后,我更改文化,并且仅针对波斯日历,我使用反射来更改日历(夏姆斯日历)。

这是此控件中的重要函数,它向你展示了如何使用波斯日历来实现它。

private void ChangeCulture()
{
DefaultCulture = new System.Globalization.CultureInfo(
    System.Threading.Thread.CurrentThread.CurrentUICulture.Name);

switch (SelectedCulture.Name)
{
case "fa-IR":
System.Globalization.DateTimeFormatInfo info = SelectedCulture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "ی", "§", "«", "چ", "پ", "¤", "¬" };
info.DayNames = new string[] { "یک¬ë ى", "§ي¬ë ى", "ﺳﻪ¬ë ى", "چىں©¬ë ى", "پ뤬ë ى",
    "¤êمى", "¬ë ى" };
info.AbbreviatedMonthNames = new string[] { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©",
    "ê©§ں§", "¬ى©یي©", "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.MonthNames = new string[] 
    { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©", "ê©§ں§", "¬ى©یي©", 
    "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.AMDesignator = "ç.â";
info.PMDesignator = " .â";
info.ShortDatePattern = "yyyy/MM/dd";
info.FirstDayOfWeek = DayOfWeek.Saturday;
System.Globalization.PersianCalendar cal = new System.Globalization.PersianCalendar();
typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "calendar", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(info, cal);

object obj = typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "m_cultureTableRecord", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).GetValue(info);

obj.GetType().GetMethod("UseCurrentCalendar",
   System.Reflection.BindingFlags.NonPublic |
   System.Reflection.BindingFlags.Instance).Invoke(obj,
   new object[] { cal.GetType().GetProperty("ID",
   System.Reflection.BindingFlags.Instance |
   System.Reflection.BindingFlags.NonPublic).GetValue(cal, null) });

typeof(System.Globalization.CultureInfo).GetField("calendar",
    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(SelectedCulture, cal);

System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat = info;
System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat = info;
break;

default:
System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
break;
}
}
© . All rights reserved.