无论操作系统文化是什么,我都想将我的应用程序文化设置为我想要的任何内容。为了获得这一点,我使用 CultureInfo 类和“fa-IR”作为文化,但它使用“GregorianCalendar”作为默认日历,而不是 .NET PersianCalendar 类。所以我尝试从 CultureInfo 派生一个新类来实现我的客户文化:
/// <summary>
/// Represents culture specific information, Persian calendar and number format info for Iran.
/// </summary>
public class PersianCultureInfo : CultureInfo
{
private Calendar _calendar = new PersianCalendar();
public PersianCultureInfo()
: base("fa-IR", true)
{
}
/// <summary>
/// Persian calendar with solar system algorithm for Iran.
/// </summary>
public override Calendar Calendar
{
get
{
return this._calendar;
}
}
public static PersianCultureInfo Create()
{
PersianCultureInfo culture = new PersianCultureInfo();
culture.PerpareDateTimeFormatInfo();
return culture;
}
private void PerpareDateTimeFormatInfo()
{
this.DateTimeFormat.Calendar = this.Calendar;
this.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
}
}
问题是 DateTimeFormat 属性引发以下异常:
Not a valid calendar for the given culture.
Parameter name: value
所以我尝试覆盖 OptionalCalendars 属性以将 PersianCalendar 添加到它们,因为默认情况下该列表仅包含 GregorianCalendar 和 HijriCalendar:
public override Calendar[] OptionalCalendars
{
get
{
return base.OptionalCalendars.Concat<Calendar>(new Calendar[1] { new PersianCalendar() }).ToArray<Calendar>();
}
}
但它并没有解决问题。怎么了?如何以正确的方式将 PersianCalendar 设置为 CultureInfo 和 DateTimeFormat 的默认日历?