更有效的方法来获取当前的一周并运行代码?

时间:2014-06-24 23:38:44

标签: c# windows-phone-8 performance

我在这里有一个非常无效的代码,它做了一个相当简单的工作 - 检测它是星期几,并用字符串设置一堆文本框。代码工作正常,但我认为有更好的方法吗?

if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
                {
                    time7.Text = "TUESDAY";
                    time8.Text = "WEDNESDAY";
                    time9.Text = "THURSDAY";
                    time10.Text = "FRIDAY";
                    time11.Text = "SATURDAY";
                    time12.Text = "SUNDAY";
                    time13.Text = "MONDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
                {
                    time7.Text = "WEDNESDAY";
                    time8.Text = "THURSDAY";
                    time9.Text = "FRIDAY";
                    time10.Text = "SATURDAY";
                    time11.Text = "SUNDAY";
                    time12.Text = "MONDAY";
                    time13.Text = "TUESDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Wednesday)
                {
                    time7.Text = "THURSDAY";
                    time8.Text = "FRIDAY";
                    time9.Text = "SATURDAY";
                    time10.Text = "SUNDAY";
                    time11.Text = "MONDAY";
                    time12.Text = "TUESDAY";
                    time13.Text = "WEDNESDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
                {
                    time7.Text = "FRIDAY";
                    time8.Text = "SATURDAY";
                    time9.Text = "SUNDAY";
                    time10.Text = "MONDAY";
                    time11.Text = "TUESDAY";
                    time12.Text = "WEDNESDAY";
                    time13.Text = "THURSDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Friday)
                {
                    time7.Text = "SATURDAY";
                    time8.Text = "SUNDAY";
                    time9.Text = "MONDAY";
                    time10.Text = "TUESDAY";
                    time11.Text = "WEDNESDAY";
                    time12.Text = "THURSDAY";
                    time13.Text = "FRIDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
                {
                    time7.Text = "SUNDAY";
                    time8.Text = "MONDAY";
                    time9.Text = "TUESDAY";
                    time10.Text = "WEDNESDAY";
                    time11.Text = "THURSDAY";
                    time12.Text = "FRIDAY";
                    time13.Text = "SATURDAY";
                }
                if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
                {
                    time7.Text = "MONDAY";
                    time8.Text = "TUESDAY";
                    time9.Text = "WEDNESDAY";
                    time10.Text = "THURSDAY";
                    time11.Text = "FRIDAY";
                    time12.Text = "SATURDAY";
                    time13.Text = "SUNDAY";
                }

4 个答案:

答案 0 :(得分:3)

var textBoxes = new List<TextBox>{time7, time8};//add others
var current = DateTime.Now;
foreach(var tb in textBoxes)
{
  current = current.AddDays(1);
  tb.Text = current.DayOfWeek.ToString().ToUpper();
}

像这样的东西,你也可以使用其他变种,比如for(var i = 0...)循环,但你明白了。

此外,您可以使用MVVM模式绑定到textboxes,这更容易。

答案 1 :(得分:1)

1)创建文本框数组

2)设置动态数字(介于0到6之间)到天数

3)然后通过循环函数填充文本框

for(i=0;i<=6:i++)
{textbox[i].text=dynamic_function_in_form_load(i);}

答案 2 :(得分:0)

怎么样:

void YourFunction()
{
    // a list of your day-names: since it seems that you are maintaining some old code, I presume that implementation of localization is out of question, so you can use a list..
    var dateNames = new List<string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    // a list of your textboxes that hold names of days
    var tbs = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7 };

    // current day
    var x = (int)DateTime.Now.DayOfWeek;

    shift(dateNames, tbs, x);
}

void shift(List<string> dates, List<TextBox> tbs, int x)
{
    for (int i = 0; i < x; i++)
    {
        dates.Insert(7, dates[0]);
        dates.Remove(dates[0]);
    }

    for (int i = 0; i < dates.Count; i++)
    {
        tbs[i].Text = dates[i];
    }
}

它不是“很好”,但仍比现在更容易维护。

希望这有帮助。

答案 3 :(得分:0)

这对我有用:

var labels =
    typeof(DayOfWeek).GetEnumNames()
        .Concat(typeof(DayOfWeek).GetEnumNames())
        .Skip((int)DateTime.Now.DayOfWeek + 1)
        .Take(7)
        .Select(x => x.ToUpperInvariant())
        .ToArray();

 time7.Text = labels[0];
 time8.Text = labels[1];
 time9.Text = labels[2];
 time10.Text = labels[3];
 time11.Text = labels[4];
 time12.Text = labels[5];
 time13.Text = labels[6];