Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Why my ViewModal shows no data

$
0
0

Hi,

I had this before in my NewItemLocation.xaml.cs file

public class LocationData
{
    public int item_location_id { get; set; }
    public string country_name { get; set; }
    public double? location_latitude { get; set; }
    public double? location_longitude { get; set; }
    public string location_address { get; set; }
    public string telephone { get; set; }
    public bool is_readonly { get; set; }
}

and referring to it using:

var client = new HttpClient();

client.BaseAddress = new Uri("https://www.my.domain/item_locations.php");

var content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("language", "EN"),
    new KeyValuePair<string, string>("item", item_id)
});

var response = await client.PostAsync("https://www.my.domain/item_locations.php", content);

var data = await response.Content.ReadAsStringAsync();

var result = JsonConvert.DeserializeObject<List<LocationData>>(data);

ObservableCollection<LocationData> trends = new ObservableCollection<LocationData>(result);

ListViewNewItemLocations.ItemsSource = trends;

BusyIndicatorNewItemLocation.IsVisible = false;

ListViewNewItemLocations.IsVisible = true;

and in my XAML:

<Label Margin="0,0,0,0" Text="{Binding country_name}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" TextColor="Black" HorizontalTextAlignment="Start" FontSize="15" LineBreakMode="WordWrap" />

everything was working perfectly.

Now i want to separate the Models in separate files so I created below file in ViewModel folder:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zeera.ViewModel
{
    public class ViewModelItemLocation : INotifyPropertyChanged
    {
        public ViewModelItemLocation()
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public int item_location_id { get; set; }
        public string country_name { get; set; }
        public double? location_latitude { get; set; }
        public double? location_longitude { get; set; }
        public string location_address { get; set; }
        public string telephone { get; set; }
        public bool is_readonly { get; set; }
    }
}

and added this in my XAML:

<?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:local="clr-namespace:Zeera.ViewModel"
             x:Name="ContentPageNewItemLocation"
             x:Class="Zeera.NewItemLocation">
    <ContentPage.BindingContext>
        <local:ViewModelItemLocation x:Name="viewModel"/>
    </ContentPage.BindingContext>

and changed the code in codebehind to:

var result = JsonConvert.DeserializeObject<List<ViewModelItemLocation>>(data);
ObservableCollection<ViewModelItemLocation> trends = new ObservableCollection<ViewModelItemLocation>(result);

but now no data is show..... :(

Why is that?

Kindly help...

Thanks,
Jassim


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>