C# Xamarin Android中REST API数据绑定到ListView

C# Xamarin Android中REST API数据绑定到ListView,c#,rest,xamarin.forms,xamarin.android,uwp-xaml,C#,Rest,Xamarin.forms,Xamarin.android,Uwp Xaml,首先,在一些上下文中,我一直在摆弄来自的Xamarin和API数据 我目前的解决方案结构如下: using Newtonsoft.Json; using System.Collections.Generic; using System.Collections.ObjectModel; using J = Newtonsoft.Json.JsonPropertyAttribute; namespace CrossPlatformTFL.Model { public partial cla

首先,在一些上下文中,我一直在摆弄来自的Xamarin和API数据

我目前的解决方案结构如下:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using J = Newtonsoft.Json.JsonPropertyAttribute;

namespace CrossPlatformTFL.Model
{
    public partial class DataModel
    {
        [J("disruptions")] public List<object> Disruptions { get; set; }
        [J("modified")] public string Modified { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("crowding")] public Crowding Crowding { get; set; }
        [J("lineStatuses")] public List<LineStatus> LineStatuses { get; set; }
        [J("id")] public string Id { get; set; }
        [J("modeName")] public string ModeName { get; set; }
        [J("routeSections")] public List<RouteSection> RouteSections { get; set; }
        [J("name")] public string Name { get; set; }
        [J("serviceTypes")] public List<ServiceType> ServiceTypes { get; set; }
    }

    public partial class Crowding
    {
        [J("$type")] public string Type { get; set; }
    }

    public partial class LineStatus
    {
        [J("id")] public long Id { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("disruption")] public Disruption Disruption { get; set; }
        [J("reason")] public string Reason { get; set; }
        [J("statusSeverityDescription")] public string StatusSeverityDescription { get; set; }
        [J("lineId")] public string LineId { get; set; }
        [J("statusSeverity")] public long StatusSeverity { get; set; }
        [J("validityPeriods")] public List<ValidityPeriod> ValidityPeriods { get; set; }
    }

    public partial class Disruption
    {
        [J("affectedStops")] public List<AffectedStop> AffectedStops { get; set; }
        [J("created")] public string Created { get; set; }
        [J("additionalInfo")] public string AdditionalInfo { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("affectedRoutes")] public List<AffectedRoute> AffectedRoutes { get; set; }
        [J("categoryDescription")] public string CategoryDescription { get; set; }
        [J("category")] public string Category { get; set; }
        [J("closureText")] public string ClosureText { get; set; }
        [J("isBlocking")] public bool? IsBlocking { get; set; }
        [J("description")] public string Description { get; set; }
        [J("isWholeLine")] public bool? IsWholeLine { get; set; }
    }

    public partial class AffectedStop
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("id")] public string Id { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class AffectedRoute
    {
        [J("id")] public string Id { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("direction")] public string Direction { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("routeSectionNaptanEntrySequence")] public List<RouteSectionNaptanEntrySequence> RouteSectionNaptanEntrySequence { get; set; }
    }

    public partial class RouteSectionNaptanEntrySequence
    {
        [J("ordinal")] public long Ordinal { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("stopPoint")] public StopPoint StopPoint { get; set; }
    }

    public partial class StopPoint
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("additionalProperties")] public List<object> AdditionalProperties { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("children")] public List<object> Children { get; set; }
        [J("icsCode")] public string IcsCode { get; set; }
        [J("hubNaptanCode")] public string HubNaptanCode { get; set; }
        [J("id")] public string Id { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("lineModeGroups")] public List<object> LineModeGroups { get; set; }
        [J("lineGroup")] public List<object> LineGroup { get; set; }
        [J("lines")] public List<object> Lines { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("modes")] public List<object> Modes { get; set; }
        [J("placeType")] public string PlaceType { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class ValidityPeriod
    {
        [J("fromDate")] public string FromDate { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("isNow")] public bool IsNow { get; set; }
        [J("toDate")] public string ToDate { get; set; }
    }

    public partial class RouteSection
    {
        [J("direction")] public string Direction { get; set; }
        [J("destination")] public string Destination { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("originator")] public string Originator { get; set; }
        [J("serviceType")] public string ServiceType { get; set; }
    }

    public partial class ServiceType
    {
        [J("name")] public string Name { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("uri")] public string Uri { get; set; }
    }

    public partial class DataModel
    {
        public static ObservableCollection<DataModel> FromJson(string json) => JsonConvert.DeserializeObject<ObservableCollection<DataModel>>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this ObservableCollection<DataModel> self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}
using CrossPlatformTFL.Model;
using System.Collections.ObjectModel;
using System.Net.Http;

namespace CrossPlatformTFL.ViewModel
{
    public class View2ViewModel
    {
        public ObservableCollection<DataModel> OpenData { get; set; } = new ObservableCollection<DataModel>();

        public View2ViewModel()
        {
            using (HttpClient hc = new HttpClient())
            {
                    //I would prefer to use `await` here but I cant figure it out either
                    var jsonString = hc.GetStringAsync("https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true").Result;
                    OpenData = DataModel.FromJson(jsonString);

                }
            }
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="CrossPlatformTFL.Android" android:icon="@drawable/icon"></application>
</manifest>

主要视图如下所示:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using J = Newtonsoft.Json.JsonPropertyAttribute;

namespace CrossPlatformTFL.Model
{
    public partial class DataModel
    {
        [J("disruptions")] public List<object> Disruptions { get; set; }
        [J("modified")] public string Modified { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("crowding")] public Crowding Crowding { get; set; }
        [J("lineStatuses")] public List<LineStatus> LineStatuses { get; set; }
        [J("id")] public string Id { get; set; }
        [J("modeName")] public string ModeName { get; set; }
        [J("routeSections")] public List<RouteSection> RouteSections { get; set; }
        [J("name")] public string Name { get; set; }
        [J("serviceTypes")] public List<ServiceType> ServiceTypes { get; set; }
    }

    public partial class Crowding
    {
        [J("$type")] public string Type { get; set; }
    }

    public partial class LineStatus
    {
        [J("id")] public long Id { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("disruption")] public Disruption Disruption { get; set; }
        [J("reason")] public string Reason { get; set; }
        [J("statusSeverityDescription")] public string StatusSeverityDescription { get; set; }
        [J("lineId")] public string LineId { get; set; }
        [J("statusSeverity")] public long StatusSeverity { get; set; }
        [J("validityPeriods")] public List<ValidityPeriod> ValidityPeriods { get; set; }
    }

    public partial class Disruption
    {
        [J("affectedStops")] public List<AffectedStop> AffectedStops { get; set; }
        [J("created")] public string Created { get; set; }
        [J("additionalInfo")] public string AdditionalInfo { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("affectedRoutes")] public List<AffectedRoute> AffectedRoutes { get; set; }
        [J("categoryDescription")] public string CategoryDescription { get; set; }
        [J("category")] public string Category { get; set; }
        [J("closureText")] public string ClosureText { get; set; }
        [J("isBlocking")] public bool? IsBlocking { get; set; }
        [J("description")] public string Description { get; set; }
        [J("isWholeLine")] public bool? IsWholeLine { get; set; }
    }

    public partial class AffectedStop
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("id")] public string Id { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class AffectedRoute
    {
        [J("id")] public string Id { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("direction")] public string Direction { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("routeSectionNaptanEntrySequence")] public List<RouteSectionNaptanEntrySequence> RouteSectionNaptanEntrySequence { get; set; }
    }

    public partial class RouteSectionNaptanEntrySequence
    {
        [J("ordinal")] public long Ordinal { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("stopPoint")] public StopPoint StopPoint { get; set; }
    }

    public partial class StopPoint
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("additionalProperties")] public List<object> AdditionalProperties { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("children")] public List<object> Children { get; set; }
        [J("icsCode")] public string IcsCode { get; set; }
        [J("hubNaptanCode")] public string HubNaptanCode { get; set; }
        [J("id")] public string Id { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("lineModeGroups")] public List<object> LineModeGroups { get; set; }
        [J("lineGroup")] public List<object> LineGroup { get; set; }
        [J("lines")] public List<object> Lines { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("modes")] public List<object> Modes { get; set; }
        [J("placeType")] public string PlaceType { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class ValidityPeriod
    {
        [J("fromDate")] public string FromDate { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("isNow")] public bool IsNow { get; set; }
        [J("toDate")] public string ToDate { get; set; }
    }

    public partial class RouteSection
    {
        [J("direction")] public string Direction { get; set; }
        [J("destination")] public string Destination { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("originator")] public string Originator { get; set; }
        [J("serviceType")] public string ServiceType { get; set; }
    }

    public partial class ServiceType
    {
        [J("name")] public string Name { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("uri")] public string Uri { get; set; }
    }

    public partial class DataModel
    {
        public static ObservableCollection<DataModel> FromJson(string json) => JsonConvert.DeserializeObject<ObservableCollection<DataModel>>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this ObservableCollection<DataModel> self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}
using CrossPlatformTFL.Model;
using System.Collections.ObjectModel;
using System.Net.Http;

namespace CrossPlatformTFL.ViewModel
{
    public class View2ViewModel
    {
        public ObservableCollection<DataModel> OpenData { get; set; } = new ObservableCollection<DataModel>();

        public View2ViewModel()
        {
            using (HttpClient hc = new HttpClient())
            {
                    //I would prefer to use `await` here but I cant figure it out either
                    var jsonString = hc.GetStringAsync("https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true").Result;
                    OpenData = DataModel.FromJson(jsonString);

                }
            }
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="CrossPlatformTFL.Android" android:icon="@drawable/icon"></application>
</manifest>

DataModel.cs
如下:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using J = Newtonsoft.Json.JsonPropertyAttribute;

namespace CrossPlatformTFL.Model
{
    public partial class DataModel
    {
        [J("disruptions")] public List<object> Disruptions { get; set; }
        [J("modified")] public string Modified { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("crowding")] public Crowding Crowding { get; set; }
        [J("lineStatuses")] public List<LineStatus> LineStatuses { get; set; }
        [J("id")] public string Id { get; set; }
        [J("modeName")] public string ModeName { get; set; }
        [J("routeSections")] public List<RouteSection> RouteSections { get; set; }
        [J("name")] public string Name { get; set; }
        [J("serviceTypes")] public List<ServiceType> ServiceTypes { get; set; }
    }

    public partial class Crowding
    {
        [J("$type")] public string Type { get; set; }
    }

    public partial class LineStatus
    {
        [J("id")] public long Id { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("disruption")] public Disruption Disruption { get; set; }
        [J("reason")] public string Reason { get; set; }
        [J("statusSeverityDescription")] public string StatusSeverityDescription { get; set; }
        [J("lineId")] public string LineId { get; set; }
        [J("statusSeverity")] public long StatusSeverity { get; set; }
        [J("validityPeriods")] public List<ValidityPeriod> ValidityPeriods { get; set; }
    }

    public partial class Disruption
    {
        [J("affectedStops")] public List<AffectedStop> AffectedStops { get; set; }
        [J("created")] public string Created { get; set; }
        [J("additionalInfo")] public string AdditionalInfo { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("affectedRoutes")] public List<AffectedRoute> AffectedRoutes { get; set; }
        [J("categoryDescription")] public string CategoryDescription { get; set; }
        [J("category")] public string Category { get; set; }
        [J("closureText")] public string ClosureText { get; set; }
        [J("isBlocking")] public bool? IsBlocking { get; set; }
        [J("description")] public string Description { get; set; }
        [J("isWholeLine")] public bool? IsWholeLine { get; set; }
    }

    public partial class AffectedStop
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("id")] public string Id { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class AffectedRoute
    {
        [J("id")] public string Id { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("direction")] public string Direction { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("routeSectionNaptanEntrySequence")] public List<RouteSectionNaptanEntrySequence> RouteSectionNaptanEntrySequence { get; set; }
    }

    public partial class RouteSectionNaptanEntrySequence
    {
        [J("ordinal")] public long Ordinal { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("stopPoint")] public StopPoint StopPoint { get; set; }
    }

    public partial class StopPoint
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("additionalProperties")] public List<object> AdditionalProperties { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("children")] public List<object> Children { get; set; }
        [J("icsCode")] public string IcsCode { get; set; }
        [J("hubNaptanCode")] public string HubNaptanCode { get; set; }
        [J("id")] public string Id { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("lineModeGroups")] public List<object> LineModeGroups { get; set; }
        [J("lineGroup")] public List<object> LineGroup { get; set; }
        [J("lines")] public List<object> Lines { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("modes")] public List<object> Modes { get; set; }
        [J("placeType")] public string PlaceType { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class ValidityPeriod
    {
        [J("fromDate")] public string FromDate { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("isNow")] public bool IsNow { get; set; }
        [J("toDate")] public string ToDate { get; set; }
    }

    public partial class RouteSection
    {
        [J("direction")] public string Direction { get; set; }
        [J("destination")] public string Destination { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("originator")] public string Originator { get; set; }
        [J("serviceType")] public string ServiceType { get; set; }
    }

    public partial class ServiceType
    {
        [J("name")] public string Name { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("uri")] public string Uri { get; set; }
    }

    public partial class DataModel
    {
        public static ObservableCollection<DataModel> FromJson(string json) => JsonConvert.DeserializeObject<ObservableCollection<DataModel>>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this ObservableCollection<DataModel> self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}
using CrossPlatformTFL.Model;
using System.Collections.ObjectModel;
using System.Net.Http;

namespace CrossPlatformTFL.ViewModel
{
    public class View2ViewModel
    {
        public ObservableCollection<DataModel> OpenData { get; set; } = new ObservableCollection<DataModel>();

        public View2ViewModel()
        {
            using (HttpClient hc = new HttpClient())
            {
                    //I would prefer to use `await` here but I cant figure it out either
                    var jsonString = hc.GetStringAsync("https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true").Result;
                    OpenData = DataModel.FromJson(jsonString);

                }
            }
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="CrossPlatformTFL.Android" android:icon="@drawable/icon"></application>
</manifest>
我的
MainPage.xaml
如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="CrossPlatformTFL.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:CrossPlatformTFL">

    <ContentPage.Content>
        <StackLayout>
            <Label Text="Page One" />
            <Label Text="Line Two" />
            <Button Clicked="Button_Clicked" Text="Go to page two" />
        </StackLayout>
    </ContentPage.Content>

</ContentPage>
我的
View2.xaml
是:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="CrossPlatformTFL.View.View2"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="View2" />
            <ListView ItemsSource="{Binding OpenData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Detail="{Binding LineStatuses[0].StatusSeverityDescription}" 
                                  Text="{Binding Name}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
因此,计划是将从API返回的数据绑定到列表视图,并在View2上显示它们

正如您在上面看到的,我不能在
View2ViewModel.cs的构造函数中使用
wait
,这是我的第一个问题

我希望你能想出一些解决这个问题的办法。

第二个问题也是关于
View2ViewModel.cs
,特别是关于

var jsonString = hc.GetStringAsync("https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true").Result;
在android上,代码在这里中断,我在调试器中没有关于此的任何信息:

现在,令人惊讶的是,代码在UWP上运行良好,并在
View2
上显示api数据:

请帮助我理解我如何等待API调用,以及为什么它在UWP上工作而不是在android上工作

您可以下载调试项目

另外,我的
AndroidManifest.xml
如下所示:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using J = Newtonsoft.Json.JsonPropertyAttribute;

namespace CrossPlatformTFL.Model
{
    public partial class DataModel
    {
        [J("disruptions")] public List<object> Disruptions { get; set; }
        [J("modified")] public string Modified { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("crowding")] public Crowding Crowding { get; set; }
        [J("lineStatuses")] public List<LineStatus> LineStatuses { get; set; }
        [J("id")] public string Id { get; set; }
        [J("modeName")] public string ModeName { get; set; }
        [J("routeSections")] public List<RouteSection> RouteSections { get; set; }
        [J("name")] public string Name { get; set; }
        [J("serviceTypes")] public List<ServiceType> ServiceTypes { get; set; }
    }

    public partial class Crowding
    {
        [J("$type")] public string Type { get; set; }
    }

    public partial class LineStatus
    {
        [J("id")] public long Id { get; set; }
        [J("created")] public string Created { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("disruption")] public Disruption Disruption { get; set; }
        [J("reason")] public string Reason { get; set; }
        [J("statusSeverityDescription")] public string StatusSeverityDescription { get; set; }
        [J("lineId")] public string LineId { get; set; }
        [J("statusSeverity")] public long StatusSeverity { get; set; }
        [J("validityPeriods")] public List<ValidityPeriod> ValidityPeriods { get; set; }
    }

    public partial class Disruption
    {
        [J("affectedStops")] public List<AffectedStop> AffectedStops { get; set; }
        [J("created")] public string Created { get; set; }
        [J("additionalInfo")] public string AdditionalInfo { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("affectedRoutes")] public List<AffectedRoute> AffectedRoutes { get; set; }
        [J("categoryDescription")] public string CategoryDescription { get; set; }
        [J("category")] public string Category { get; set; }
        [J("closureText")] public string ClosureText { get; set; }
        [J("isBlocking")] public bool? IsBlocking { get; set; }
        [J("description")] public string Description { get; set; }
        [J("isWholeLine")] public bool? IsWholeLine { get; set; }
    }

    public partial class AffectedStop
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("id")] public string Id { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class AffectedRoute
    {
        [J("id")] public string Id { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("direction")] public string Direction { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("routeSectionNaptanEntrySequence")] public List<RouteSectionNaptanEntrySequence> RouteSectionNaptanEntrySequence { get; set; }
    }

    public partial class RouteSectionNaptanEntrySequence
    {
        [J("ordinal")] public long Ordinal { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("stopPoint")] public StopPoint StopPoint { get; set; }
    }

    public partial class StopPoint
    {
        [J("lat")] public long Lat { get; set; }
        [J("commonName")] public string CommonName { get; set; }
        [J("additionalProperties")] public List<object> AdditionalProperties { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("children")] public List<object> Children { get; set; }
        [J("icsCode")] public string IcsCode { get; set; }
        [J("hubNaptanCode")] public string HubNaptanCode { get; set; }
        [J("id")] public string Id { get; set; }
        [J("lon")] public long Lon { get; set; }
        [J("lineModeGroups")] public List<object> LineModeGroups { get; set; }
        [J("lineGroup")] public List<object> LineGroup { get; set; }
        [J("lines")] public List<object> Lines { get; set; }
        [J("naptanId")] public string NaptanId { get; set; }
        [J("stationNaptan")] public string StationNaptan { get; set; }
        [J("modes")] public List<object> Modes { get; set; }
        [J("placeType")] public string PlaceType { get; set; }
        [J("status")] public bool Status { get; set; }
    }

    public partial class ValidityPeriod
    {
        [J("fromDate")] public string FromDate { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("isNow")] public bool IsNow { get; set; }
        [J("toDate")] public string ToDate { get; set; }
    }

    public partial class RouteSection
    {
        [J("direction")] public string Direction { get; set; }
        [J("destination")] public string Destination { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("destinationName")] public string DestinationName { get; set; }
        [J("originationName")] public string OriginationName { get; set; }
        [J("name")] public string Name { get; set; }
        [J("originator")] public string Originator { get; set; }
        [J("serviceType")] public string ServiceType { get; set; }
    }

    public partial class ServiceType
    {
        [J("name")] public string Name { get; set; }
        [J("$type")] public string Type { get; set; }
        [J("uri")] public string Uri { get; set; }
    }

    public partial class DataModel
    {
        public static ObservableCollection<DataModel> FromJson(string json) => JsonConvert.DeserializeObject<ObservableCollection<DataModel>>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this ObservableCollection<DataModel> self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}
using CrossPlatformTFL.Model;
using System.Collections.ObjectModel;
using System.Net.Http;

namespace CrossPlatformTFL.ViewModel
{
    public class View2ViewModel
    {
        public ObservableCollection<DataModel> OpenData { get; set; } = new ObservableCollection<DataModel>();

        public View2ViewModel()
        {
            using (HttpClient hc = new HttpClient())
            {
                    //I would prefer to use `await` here but I cant figure it out either
                    var jsonString = hc.GetStringAsync("https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true").Result;
                    OpenData = DataModel.FromJson(jsonString);

                }
            }
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="CrossPlatformTFL.Android" android:icon="@drawable/icon"></application>
</manifest>

公共部分类视图2:ContentPage
{
View2ViewModel vm=null;
公众意见2()
{
初始化组件();
}
公共重写异步void OnAppearing(){
if(vm==null){
vm=新的View2ViewModel();
等待vm.FetchData();
BindingContext=vm;
}
}
}
公共类View2ViewModel
{
公共ObservableCollection OpenData{get;set;}=new ObservableCollection();
公共异步任务FetchData()
{
使用(HttpClient hc=new HttpClient())
{
var jsonString=await hc.GetStringAsync(“https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true");
OpenData=DataModel.FromJson(jsonString);
}
}
}
}
公共部分类视图2:ContentPage
{
View2ViewModel vm=null;
公众意见2()
{
初始化组件();
}
公共重写异步void OnAppearing(){
if(vm==null){
vm=新的View2ViewModel();
等待vm.FetchData();
BindingContext=vm;
}
}
}
公共类View2ViewModel
{
公共ObservableCollection OpenData{get;set;}=new ObservableCollection();
公共异步任务FetchData()
{
使用(HttpClient hc=new HttpClient())
{
var jsonString=await hc.GetStringAsync(“https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr%2C%20overground/Status?detail=true");
OpenData=DataModel.FromJson(jsonString);
}
}
}
}

每当出现未处理的异常时,添加一个try/catch来处理它,并捕获发生的特定异常类型/消息。。。但我还是无法理解?您也可以单击“复制详细信息”链接,将生成的完整堆栈跟踪粘贴到其他地方。在您的Android项目属性中,尝试更改HttpClientImplementation@Jason将它改为android,工作起来很有魅力!!谢谢你,先生。但是我该如何进行并使所有的异常都是异步的呢?每当您有一个未处理的异常时,添加一个try/catch来处理它,并捕获正在发生的特定异常类型/消息。。。但我还是无法理解?您也可以单击“复制详细信息”链接,将生成的完整堆栈跟踪粘贴到其他地方。在您的Android项目属性中,尝试更改HttpClientImplementation@Jason将它改为android,工作起来很有魅力!!谢谢你,先生。但是我该怎么做才能使所有的程序都是异步的呢?谢谢Jason。。不得不将访问修饰符更改为
覆盖async void OnAppearing()
,之后一切都很顺利。。必须将访问修饰符更改为
覆盖async void OnAppearing()
,之后一切都很顺利