有人能解释一下什么新字符串[,]{{L0,L1},{D0,D1}}[thc,0]吗;正在做什么

  • 本文关键字:thc D1 D0 能解释 L1 字符串 L0 一下 c#
  • 更新时间 :
  • 英文 :


我多次使用此代码来设置SVG的值:

Current.Resources["TuesdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];

我试着这样清理代码:

var L0 = _resourcesPath + "uncheck_Light.svg";
var L1 = _resourcesPath + "checked_Light.svg";
var D0 = _resourcesPath + "uncheck_Dark.svg";
var D1 = _resourcesPath + "checked_Dark.svg";
Current.Resources["MondayActiveStatusIcon"] = new string[,] { { L0, L1 }, { D0, D1 } }[thc, 0];

我想通过将{ L0, L1 }, { D0, D1 }分配到一个变量中并使用它来清理它。

有人能解释一下我是怎么做到的吗?

这是完整的代码供参考。我想做的是简化它,因为它现在看起来非常复杂:

var L0 = _resourcesPath + "uncheck_Light.svg";
var L1 = _resourcesPath + "checked_Light.svg";
var D0 = _resourcesPath + "uncheck_Dark.svg";
var D1 = _resourcesPath + "checked_Dark.svg";
Current.Resources["MondayActiveStatusIcon"] = new string[,] { { L0, L1 }, { D0, D1 } }[thc, 0];
Current.Resources["TuesdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
Current.Resources["WednesdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
Current.Resources["ThursdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
Current.Resources["FridayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
Current.Resources["SaturdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
Current.Resources["SundayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 0];
var viewHistoryList = App.DB.GetViewHistory(TIME2.Week.FromDate());
foreach (var item in viewHistoryList)
{
var DD = Convert.ToInt32(item.YYMMDD.Substring(4, 2));
var MM = Convert.ToInt32(item.YYMMDD.Substring(2, 2));
var YY = Convert.ToInt32(item.YYMMDD.Substring(0, 2));
DateTime dt = new DateTime(YY, MM, DD);
switch (dt.DayOfWeek)
{
case DayOfWeek.Monday:
Current.Resources["MondayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Tuesday:
Current.Resources["TuesdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Wednesday:
Current.Resources["WednesdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Thursday:
Current.Resources["ThursdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Friday:
Current.Resources["FridayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Saturday:
Current.Resources["SaturdayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
case DayOfWeek.Sunday:
Current.Resources["SundayActiveStatusIcon"] = new string[,] { { _resourcesPath + "uncheck_Light.svg", _resourcesPath + "checked_Light.svg" }, { _resourcesPath + "uncheck_Dark.svg", _resourcesPath + "checked_Dark.svg" } }[thc, 1];
break;
default:
break;
}
}

这是干编码的,但我会这样做,完全跳过奇怪的数组技巧:

// figure out the icon paths based on the current theme
var theme = (thc ? "Light" : "Dark");
var uncheckIcon = _resourcesPath + "uncheck_" + theme + ".svg";
var checkIcon = _resourcesPath + "checked_" + theme + ".svg";
// gather up a set of active dates
var viewHistoryList = App.DB.GetViewHistory(TIME2.Week.FromDate());
var daysActive = new HashSet<DayOfWeek>();
foreach (var item in viewHistoryList)
{
var DD = Convert.ToInt32(item.YYMMDD.Substring(4, 2));
var MM = Convert.ToInt32(item.YYMMDD.Substring(2, 2));
var YY = Convert.ToInt32(item.YYMMDD.Substring(0, 2));
daysActive.Add(new DateTime(YY, MM, DD).DayOfWeek);
}
// set the icons in the resource collection based on the set of found dates
Current.Resources["MondayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Monday)? checkIcon : uncheckIcon;
Current.Resources["TuesdayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Tuesday)? checkIcon : uncheckIcon;
Current.Resources["WednesdayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Wednesday)? checkIcon : uncheckIcon;
Current.Resources["ThursdayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Thursday)? checkIcon : uncheckIcon;
Current.Resources["FridayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Friday)? checkIcon : uncheckIcon;
Current.Resources["SaturdayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Saturday)? checkIcon : uncheckIcon;
Current.Resources["SundayActiveStatusIcon"] = daysActive.Contains(DayOfWeek.Sunday)? checkIcon : uncheckIcon;

最新更新