none
Why my ViewModal shows no data RRS feed

  • Question

  • 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


    Sunday, April 21, 2019 8:48 AM

All replies

  • Hi Jassim Rahma,

    Thank you for posting here.

    Since your question is more related to Xamarin forum, I will move it to Xamarin forum for suitable support.

    https://forums.xamarin.com/

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 22, 2019 5:22 AM