Xamarin - Displaying JSON
I'm trying to do a simple application to get a JSON and display the values on a collection.
I have a response, a model of the desired object to deserialize and a service to perform the deserialization.
It provides an error related to the property from the model I'm trying to display like:
Binding: 'Symbol' property not found on 'XamarinApp.', target property: 'Xamarin.Forms.Label.Text'
But as far as I can see the property exists. Which leads me to believe the issue is with the view?
1 Reply
On my page I'm doing:
On the ViewModel:
Sorry for bumping so early, but still haven't figured this out, would appreciate any help!
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
Title="{Binding Title}"
x:Class="XamarinApp.Views.TickersPage">
<StackLayout Padding="5">
<CollectionView ItemsSource="{Binding Tickers}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0" VerticalOptions="Center">
<Label Text="{Binding Symbol}"
FontAttributes="Bold"
FontSize="Medium"
LineBreakMode="TailTruncation"
/>
</StackLayout>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
Title="{Binding Title}"
x:Class="XamarinApp.Views.TickersPage">
<StackLayout Padding="5">
<CollectionView ItemsSource="{Binding Tickers}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0" VerticalOptions="Center">
<Label Text="{Binding Symbol}"
FontAttributes="Bold"
FontSize="Medium"
LineBreakMode="TailTruncation"
/>
</StackLayout>
public class TickersPageViewModel : ViewModelBase
{
private readonly IApiService _apiService;
private List<Ticker> _tickers;
public TickersPageViewModel(INavigationService navigationService, IApiService apiService) : base(navigationService)
{
_apiService = apiService;
Title = "Tickers Page";
LoadTickersAsync();
}
public List<Ticker> Tickers
{
get => _tickers;
set => SetProperty(ref _tickers, value);
}
private async void LoadTickersAsync()
{
string url = App.Current.Resources["binance"].ToString();
Response response = await _apiService.GetResultAsync<Ticker>(url, "/api", "/v3/ticker/24hr");
if (!response.Success)
{
await App.Current.MainPage.DisplayAlert("Error", response.Message, "Accept");
return;
}
Tickers = (List<Ticker>)response.Result;
}
}
public class TickersPageViewModel : ViewModelBase
{
private readonly IApiService _apiService;
private List<Ticker> _tickers;
public TickersPageViewModel(INavigationService navigationService, IApiService apiService) : base(navigationService)
{
_apiService = apiService;
Title = "Tickers Page";
LoadTickersAsync();
}
public List<Ticker> Tickers
{
get => _tickers;
set => SetProperty(ref _tickers, value);
}
private async void LoadTickersAsync()
{
string url = App.Current.Resources["binance"].ToString();
Response response = await _apiService.GetResultAsync<Ticker>(url, "/api", "/v3/ticker/24hr");
if (!response.Success)
{
await App.Current.MainPage.DisplayAlert("Error", response.Message, "Accept");
return;
}
Tickers = (List<Ticker>)response.Result;
}
}