Может кто-нибудь объяснить, что такое новая строка [,] {{L0, L1}, {D0, D1}} [th c, 0]; делается? - PullRequest
0 голосов
/ 07 апреля 2020

Я использую этот код несколько раз для установки значений 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;
            }
        }

1 Ответ

2 голосов
/ 07 апреля 2020

Это кодируется dry, но я бы сделал что-то вроде этого и вообще пропустил бы странные трюки с массивами:

// 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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...