绑定在WP8中的MVVM中不起作用

时间:2013-06-24 16:34:06

标签: windows-phone-8 windows-phone

我是WP8开发的新手。我已经关注了几个星期的在线课程,该课程的第二项任务是开发一个应用程序来显示天气,一些新闻和与城市相关的照片。 到目前为止,我已经使用Panorama控件开发了遵循MVVM模式的应用程序,作为我需要显示的不同内容的上诉者。 不再这样,我面临的问题是显示从webservices检索的xml数据。

XAML是:

<phone:panorama x:Name="myPanorama"
            DataContext = {Binding Source="WeatherViewModel"}>
   <PanoramaItem header="MyWeather">
      <Textblock x:name="txtCity"
       Text = {Binding Weather.City}
      </Textblock>       
   </PanoramaItem>
   <panoramaItem header="Config">
      <Text x:Name="txtGetCity"/>
      <Button x:Name="btnGetCity"
              Command={Binding GetWeatherCommand}/>
   </panoramaItem>
</phone:panorama>

我的ViewModel:

public class WeaterViewModel : NotificationEnableObject
{
   private Weather _currentWeather;        
   public Weather GetCurrentWeather
   {
        get
        {
            if (_currentWeather == null)
                _currentWeather = new Weather();

            return _currentWeather;
        }
        set { _currentWeather = value;
        OnPropertyChanged("GetCurrentWeather"); 
        }
 }

//Constructor        ServiceModel serviceModel = new ServiceModel();
 public WeatherViewModel()
 {
        serviceModel.GetWeatherCompleted += (s, a) =>
        {
            _currentWeather = new Clima();
            _currentWeather.City= a.Results[0].City;
            _currentWeather.tempC = a.Results[0].tempC;
        };

        getWeatherCommand = new ActionCommand(null);
  }

   ActionCommand getWeatherCommand;  // ActionCommand derivied from ICommand      
   public ActionCommand GetWeatherCommand
    {
        get
        {
            if (getWeatherCommand!= null)
            {
                getWeatherCommand = new ActionCommand(() =>
                    {
                        //Call the Service who retrieved the data
                    });
            }

            return getWeatherCommand;
        }
    }

}

指定的天气是包含City属性的公共类。我尝试过使用IObservableCollention,结果是一样的: - (

正如您在全景控制中看到的,我有2个部分。我写的那个我想看的城市,以及我从网络服务中获取信息的部分。

任何线索或帮助都会非常感激

问候!

1 个答案:

答案 0 :(得分:0)

好的,我觉得这很容易解决。 您以这种方式设置GetCurrentWeather


_currentWeather = new Clima();
_currentWeather.City= a.Results[0].City;
_currentWeather.tempC = a.Results[0].tempC;

这不会触发PropertyChanged事件。将其更改为:


GetCurrentWeather= new Clima();
GetCurrentWeather.City= a.Results[0].City;
GetCurrentWeather.tempC = a.Results[0].tempC;

你应该没事。

相关问题