微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我该如何清理?

如何解决我该如何清理?

| 我正在使用利用Google Weather API的应用程序,现在我得到了一些真正的丑陋代码,只是用来填充3天预报,看起来像这样(记住不要笑)
groupBox3.Text = set.Forecast[1].DayOfTheWeek;
label4.Text = string.Format(\"High {0}\",set.Forecast[1].High);
label3.Text = string.Format(\"Low: {0}\",set.Forecast[1].Low);
label11.Text = string.Format(\"Condition: {0}\",set.Forecast[1].Condition);
pictureBox1.Load(set.Forecast[1].Icon);

groupBox4.Text = set.Forecast[2].DayOfTheWeek;
label14.Text = string.Format(\"High {0}\",set.Forecast[2].High);
label13.Text = string.Format(\"Low: {0}\",set.Forecast[2].Low);
label20.Text = string.Format(\"Condition: {0}\",set.Forecast[2].Condition);
pictureBox2.Load(set.Forecast[2].Icon);

groupBox5.Text = set.Forecast[3].DayOfTheWeek;
label7.Text = string.Format(\"High {0}\",set.Forecast[3].High);
label6.Text = string.Format(\"Low: {0}\",set.Forecast[3].Low);
label11.Text = string.Format(\"Condition: {0}\",set.Forecast[3].Condition);
pictureBox3.Load(set.Forecast[3].Icon);
我告诉你这很丑。我想做的是有一个循环(和可能的泛型)来完成与此丑陋代码相同的任务。我尝试了几种不同的方法,但是它们总是失败。天气像这样产生
WeatherSet set = WeatherService.Response(GoogleWeatherRequest.RequestData(new WeatherService(\"99109\")));
在变量集中有用于当前信息和3天预测的信息,但是像这样运行它会使我发疯,因为它是如此笨拙且效率不高。因此,有人能以任何有效的方式完成此任务吗?     

解决方法

        我认为您应该为此创建自己的自定义控件ForecastView,其中具有组框,三个标签和图片框。让它有一个名为
Forecast
的属性,并将逻辑填充到其设置器中的字段中。这样,表单的代码将非常干净,例如
forecastView1.ForeCast = set.Forecast[1];
forecastView2.ForeCast = set.Forecast[2];
// etc
好像您已经将字段放入组框一样,放置也不成问题。 另外,如果有一天您需要以另一种形式甚至在另一个应用程序中呈现此数据,则将其全部置于单独的控件中将为您提供许多代码重用选项。更不用说,如果您决定将pictureBox放置在左侧而不是右侧,则可以一次更改所有预测的外观。     ,        您的代码没有什么低效的。它\“S有点难过重复自己,但有\”没什么真的错了吧。 如果要避免重复,只需将控件放在数组中即可执行以下操作:
for (index in number of forecasts) {
   groupBox[index].Text = set.Forecast[index]....;
   hiLabel[index].Text  = ...:
   lowLabel[index].Text = ...:
   ...
}
    ,        您是否尝试过对控件进行分组或即时生成控件?这样,您就不必索引到“预测”列表中,您只需枚举集合即可,而不必为每次迭代使用不同的控件,而只需填充单个集合即可。
foreach (var item in set.Forecast.Take(3))
{
    var groupBox = new GroupBox(); // it don\'t if the class is actually called GroupBox...
    groupBox.Text = item.DayOfTheWeek;
    labelHigh.Text = string.Format(\"High {0}\",item.High);
    labelLow.Text = string.Format(\"Low: {0}\",item.Low);
    labelCondition.Text = string.Format(\"Condition: {0}\",item.Condition);
    pictureBox.Load(item.Icon);
    groupBox.Controls.Add(labelHigh)
    ...
    this.Controls.Add(groupBox);
}
上面的代码只是为了给您一个想法。 实际上,您的代码并不会出错,我真正唯一缺少的是断言您的索引没有超出范围。     ,        这就是我最后的解决方案(感谢@dyppl)。对于用户控制的想法
public partial class forecastView : UserControl
{    
    public forecastView()
    {
        InitializeComponent();
    }

    public forecastView(int x,int y,int index,WeatherSet set)
    {
        InitializeComponent();

        label7.Text = string.Format(\"High:{0}\",set.Forecast[index].High);
        label8.Text = string.Format(\"Low: {0}\",set.Forecast[index].Low);
        pictureBox3.Load(string.Format(\"http://www.google.com/{0}\",set.Forecast[index].Icon));
        groupBox1.Text = set.Forecast[index].DayOfTheWeek;

        this.Location = new System.Drawing.Point(x,y);
    }
}
我以这种方式加载它们
private void LoadControls(WeatherSet set)
{
    RemoveConrols();
    //form2.Dispose();

    form = new forecastView(12,136,1,set);
    form1 = new forecastView(155,2,set);
    form2 = new forecastView(12,218,3,set);

    this.Controls.Add(form);
    this.Controls.Add(form1);
    this.Controls.Add(form2);
}
因此,感谢所有帮助我解决此问题的人;)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。