In my xaml code I added my class "Feed" to my resources. Like this:
<Page.Resources>
<data:Feed x:Key="Feed"></data:Feed>
</Page.Resources>
The class contains the property Apod and a method that later updates the property.
private ApodModel _apod;
public ApodModel Apod
{
get { return _apod; }
set { _apod = value; }
}
public Feed()
{
DownloadApod();
}
private async void DownloadApod()
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(new Uri("http://spacehub.azurewebsites.net/api/apod", UriKind.Absolute));
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Apod = JsonConvert.DeserializeObject<ApodModel>(json);
var apod = new AppSettings<ApodModel>();
await apod.SaveAsync("Apod", Apod);
}
}
}
catch (Exception)
{
}
}
In my XAML my binding to the property looks like this:
<StackPanel DataContext="{StaticResource Feed}">
<TextBlock Text="{Binding Apod.Description}">
</StackPanel>
When I debug the property Apod gets updated but it doesn't change in the XAML. What am I doing wrong?
You need to notify the view when the "Apod" property changes (otherwise, it will read the property value initially as its default value null
, and never again). To do this, have your "Feed" class implement INotifyPropertyChanged
, and raise the PropertyChanged
event in the "Apod" property setter.
You need to use implement your class using INotifyPropertyChange
and them implement interface.
private ApodModel _apod;
public ApodModel Apod
{
get { return _apod; }
set { _apod = value;
NotifyPropertyChange("Apod");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChange(string name)
{
if(PropertyChanged!=null)
{
PropertyChanged(this,new PropertyChangedEventArgs(name));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.