2011-06-23 173 views
0

我正在使用多個Weather API。在谷歌API的我正在繼續加載3天的預測。這是怎麼回事當前外觀: 編輯:此問題已得到解決在運行時不添加控件

private void LoadForecast(WeatherSet set) 
     { 
      RemoveControls(); 

      form = new forecastView(7, 159, 1, set); 
      form1 = new forecastView(161, 159, 2, set); 
      form2 = new forecastView(7, 254, 3, set); 

      this.Controls.Add(form); 
      this.Controls.Add(form1); 
      this.Controls.Add(form2); 
     } 




using System.Windows.Forms; 
using WeatherVaneGoogleWrapper.Forecast; 
using WeatherVaneGoogleWrapper.Weather; 

namespace WeatherVane 
{ 
    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; 
      label9.Text = string.Format("Conditions: {0}", set.Forecast[index].Condition); 
      this.Location = new System.Drawing.Point(x, y); 
     } 
    } 
} 

這是好得多,如果我說我自己,這裏怎麼嘗試,但就是不能把我tworking:\

private void LoadCurrentWeatherData(string loc) 
      { 

       WeatherSet set = WeatherService.Response(GoogleWeatherRequest.RequestData(new WeatherService(loc))); 

       LowLabelOne.Text = string.Format("Current Temp: {0}{1}", set.Current.TemperatureFahrenheit, "°"); 
       groupBox1.Text = string.Format("Current Conditions for {0}", set.Information.City); 
       HumidityLabel.Text = set.Current.Humidity; 
       windConditionsLabel.Text = string.Format("Wind Conditions: {0}", set.Current.Wind); 
       label3.Text = string.Format("Condition: {0}", set.Current.Condition); 
       pictureBox1.Load(string.Format("http://www.google.com/{0}", set.Current.Icon)); 

       LoadForecastControls(set); 

       dateTimeLabel.Text = string.Format("Last Check: {0} {1}", Convert.ToDateTime(set.Information.CurrentDateTime).ToShortDateString(), 
        Convert.ToDateTime(set.Information.CurrentDateTime).ToShortTimeString()); 

       set = null; 
      } 

      private void LoadForecastControls(WeatherSet set) 
      { 
       RemoveControls(); 

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

       this.Controls.Add(form); 
       this.Controls.Add(form1); 
       this.Controls.Add(form2); 
      } 

      private void RemoveControls() 
      { 
       this.Controls.Remove(form); 
       this.Controls.Remove(form1); 
       this.Controls.Remove(form2); 
      } 

Can someone help? 

回答

0

我的WebForms越來越生鏽,但我不認爲你可以新建UserControls並以這種方式添加它們。我相信你需要使用Page.LoadControl()

var form = Page.LoadControl(typeof(forecastView), new { 12, 136, 1, set }); 

Page.Controls.Add(form); 
+0

它是否也適用於MVC? – PsychoCoder

+0

在MVC中,.ASCX partials共享相同的文件擴展名,但它們根本就不是UserControl。我不認爲你可以在WebForms和MVC中使用相同的方法。 –

相關問題