Xamarin Forms ListView not displaying data in iOS, HttpClient

361 Views Asked by At

I have a Xaml Page ListView that displays the data correctly in UWP, but does not display the data correctly in iOS. I am using HttpClient to get the data from a localhost webapi connected to SQL Server running on the local machine. I think that this has something to do with the NSUrlSession setting working with HttpClient. Any Suggestions? This is the Xaml Page.

<?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:telerikDataControls="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
             xmlns:viewModels="clr-namespace:SxQiApp.ViewModels;assembly=SxQiApp"
             xmlns:telerikListView="clr-namespace:Telerik.XamarinForms.DataControls.ListView;assembly=Telerik.XamarinForms.DataControls"
             x:Class="SxQiApp.DailyCasePage">

    <ContentPage.BindingContext>
        <viewModels:VSxCaseViewModel />
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <telerikDataControls:RadListView x:Name="listView"
                                         ItemsSource="{Binding SxCases}"
                                         ItemTapped="listView_ItemTappedAsync"                                      
                                         SelectedItem="{Binding SelectedSxCase, Mode=TwoWay}">
            <telerikDataControls:RadListView.ItemTemplate>
                <DataTemplate>
                    <telerikListView:ListViewTemplateCell>
                        <telerikListView:ListViewTemplateCell.View>
                            <StackLayout>
                                <Label Text="Surgery Cases" />
                                <Label Text="{Binding Facesheet}" />
                                <Label Text="{Binding Record}" />
                            </StackLayout>
                        </telerikListView:ListViewTemplateCell.View>
                    </telerikListView:ListViewTemplateCell>
                </DataTemplate>
            </telerikDataControls:RadListView.ItemTemplate>
        </telerikDataControls:RadListView>
    </ContentPage.Content>
</ContentPage>

This is the code behind.

using System.Threading.Tasks;
using SxQiApp.Models;
using SxQiApp.ViewModels;
using Xamarin.Forms;

namespace SxQiApp
{
    // XamlC is set in App.xaml.cs
    //[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DailyCasePage : ContentPage
    {
        public DailyCasePage()
        {
            InitializeComponent();
        }

        private async void listView_ItemTappedAsync(object sender, Telerik.XamarinForms.DataControls.ListView.ItemTapEventArgs e)
        {
            var sxcase = e.Item as SxCase;

            if (sxcase != null)
            {
                var SxCaseViewModel = new SxCaseViewModel();

                if (SxCaseViewModel != null)
                {
                    SxCaseViewModel.SelectedSxCase = sxcase;

                    await Navigation.PushAsync(new EditSxCasePage(SxCaseViewModel));
                }
            }
        }
    }
}

This is the ViewModel.

using SxQiApp.Models;
using SxQiApp.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace SxQiApp.ViewModels
{
    public class VSxCaseViewModel : INotifyPropertyChanged
    {
        private List<SxCase> _sxcase;
        private VSxCaseDataService _vsxCaseDataService = new VSxCaseDataService();

        private SxCase _selectedSxCase;

        public SxCase SelectedSxCase
        {
            get { return _selectedSxCase; }
            set
            {
                _selectedSxCase = value;
                OnPropertyChanged();
            }
        }

        public List<SxCase> SxCases
        {
            get
            {
                return _sxcase;
            }
            set
            {
                _sxcase = value;
                OnPropertyChanged();
            }
        }

        public ICommand AddRecordCommand => new Command(async () =>
        {

            await _vsxCaseDataService.PostSxCase(SelectedSxCase);

        });

        //public ICommand EditSxCaseCommand => new Command(async () =>
        //{

        //    await _vsxCaseDataService.PutSxCase(SelectedSxCase);

        //});

        public Command EditSxCaseCommand
        {
            get
            {
                return new Command(async () =>
                {
                    var sxCaseDataService = new SxCaseDataService();
                    await sxCaseDataService.PutSxCase(_selectedSxCase.Id, _selectedSxCase);
                });
            }
        }



        public VSxCaseViewModel()
        {
            SelectedSxCase = new SxCase();
            GetSxCases();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private async Task GetSxCases()
        {


            SxCases = await _vsxCaseDataService.GetSxCases();
        }


    }
}

This is the service code

using Newtonsoft.Json;
using SxQiApp.Models;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace SxQiApp.Services
{
    class VSxCaseDataService
    {
        private string Url = "http://localhost:19952/api/vsxcases/";

        public async Task<List<SxCase>> GetSxCases()
        {
            var httpClient = new HttpClient();

            var json = await httpClient.GetStringAsync(Url);

            var sxcases = JsonConvert.DeserializeObject<List<SxCase>>(json);


            return sxcases;
        }

        public async Task PostSxCase(SxCase sxcase)
        {
            var httpClient = new HttpClient();

            var json = JsonConvert.SerializeObject(sxcase);

            StringContent content = new StringContent(json);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var result = await httpClient.PostAsync(Url, content);

        }

        public async Task PutSxCase(int sxcaseid, SxCase selectedSxCase)
        {
            var httpClient = new HttpClient();

            var json = JsonConvert.SerializeObject(selectedSxCase);

            StringContent content = new StringContent(json);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var result = await httpClient.PutAsync(Url + sxcaseid, content);

        }

    }
}
1

There are 1 best solutions below

0
On

Try to do real clean (delete bin & obj folder) for each project & right click on solution & choose clean {your solution name}. After it is done, right click on solution again & choose rebuild {your solution name}. After all done, try to debug on iOS11 simulator/ device.

If option no.1 is not working, maybe you can try to do breakpoint in finished launching method to see is it everything is working.

If option no.2 is not working,Maybe splash screen is getting displayed for longer duration you can reduce the duration of splash screen.