对于给定的Culture.parameter名称:不是有效的日历。值可能是PC或Windows问题?

时间:2019-04-23 07:10:02

标签: c#

我遇到DataGridView的问题,无法将Date显示为波斯日历。使用此代码后:

using System.Globalization;
using System.Threading;

...

CultureInfo info = new CultureInfo("fa-Ir")
{
    DateTimeFormat = {Calendar = new PersianCalendar()}
};

Thread.CurrentThread.CurrentCulture = info;

我的问题已解决,我的PC或笔记本电脑都没有问题,都是Windows 10和.netFramwork 4.8

我的装有Windows 7的客户PC仅具有文化方面的问题。为什么?

异常消息: 对于给定的区域性不是有效的日历。参数名称:值

1 个答案:

答案 0 :(得分:0)

我确实用以下代码替换了旧代码:

        System.Globalization.CultureInfo calture = new System.Globalization.CultureInfo("fa-Ir");
        System.Globalization.DateTimeFormatInfo info = calture.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();

        var field = typeof(System.Globalization.DateTimeFormatInfo).GetField("calendar", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        field?.SetValue(info, cal);
        var fieldInfo = typeof(System.Globalization.DateTimeFormatInfo).GetField("m_cultureTableRecord", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (fieldInfo != null)
        {
            object obj = fieldInfo.GetValue(info);
            var methodInfo = obj.GetType().GetMethod("UseCurrentCalendar", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (
                methodInfo !=
                null)
            {
                var propertyInfo = cal.GetType().GetProperty("ID", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                if (
                    propertyInfo !=
                    null)
                    methodInfo.Invoke(obj, new object[] { propertyInfo.GetValue(cal, null) });
            }
        }
        var field1 = typeof(System.Globalization.CultureInfo).GetField("calendar", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        field1?.SetValue(calture, cal);
        var fieldInfo1 = typeof(System.Globalization.CultureInfo).GetField("calendar", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        fieldInfo1?.SetValue(calture, cal);

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

,并且在所有计算机上都运行良好... 但是我不知道..如果某些代码正确,那么应该在所有计算机上工作!否则,如果有问题,则不应在任何计算机上工作...