簡體   English   中英

將項目.net目標框架從3.5升級到4.5后,日歷中的C#錯誤

[英]C# Error in calendar after upgrading project .net target framework from 3.5 to 4.5

我具有日歷的以下構建文化功能:

private void buildCulture(string culture, string calendarType)
{

    CultureInfo ci=null;
    try
    {
        if (string.IsNullOrEmpty(culture))
        {
            ci = System.Threading.Thread.CurrentThread.CurrentCulture;
        }
        else
        {
            try
            {
                ci = new CultureInfo(culture);
            }
            catch (System.ArgumentException)
            {
                ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            }
        }

        // Calendar is from system.windows.control.calendar, 
        // ci is from system.globalization.calendar:
        Calendar originalCal = ci.Calendar;

        if (!string.IsNullOrEmpty(culture) || 
             originalCal.ToString().Replace("System.Globalization.", "") != culture)
        {
            foreach (Calendar supportCal in ci.OptionalCalendars)
            {
                if (calendarType == 
                      supportCal.ToString().Replace("System.Globalization.", ""))
                {
                    ci.DateTimeFormat.Calendar = supportCal;
                }
            }
        }
    }
}

該功能在.net 3.5版中仍然有效。 但是,當我升級到.net 4.5之后, Calendar originalCal = ci.Calendar行出現錯誤,該行顯示: Calendar is an ambiguous reference between system.windows.control.calendar and system globalization.calendar

我們如何解決這些家伙?

在聲明對象時,您可能想要使用全限定名,如下所示:

System.Globalization.Calendar originalCal = ci.Calendar;

或者,如果您知道沒有使用System.Windows.Control.Calendar類型,則可以using以下命令using Calendar類型別名為System.Globalization的類型:

using Calendar = System.Globalization.Calendar;

如果我理解正確,則可能要使用system.globalization.calendar ,因為您似乎正在使用日期格式。

system.windows.control.calendar不包含日歷數據 ,而是包含日歷控件 -呈現為日歷的圖形。

為此,請刪除任何引用system.windows.control.calendar import語句。

如果那是不可能的(如果您正在System.windows.control下使用其他功能,那么Calendar會隱式包含在內),然后為system.globalization命名空間聲明一個別名

using glob = System.Globalization;

這將glob定義為名稱空間System.Globalization縮寫,這將允許您如下引用正確的Calendar類:

var ci = new glob.CultureInfo(culture);
glob.Calendar myCal = ci.Calendar;

PS:如果願意,您可以將其縮短得更多,但是就我個人而言,我認為任何比glob短的內容都會使它的含義變得不清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM