Xamarin 表单:未处理的异常:System.Reflection.TargetInvocationException:调用目标已抛出异常

Xamarin forms: Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation

当我在真实的 iPhone 设备上测试我的应用程序时,我遇到了异常。但同样的功能在 android 设备和 ios 模拟器上运行良好。

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Number.Parsing.cs:1781 at System.Number.ParseInt32 (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Number.Parsing.cs:213 at System.Int32.Parse (System.String s) [0x0000a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Int32.cs:109 at MyProject.eventsHB.get_finalDay () [0x00060] in /Users/pagematics/Projects/MyProject-social-app/MyProject/MyProject/Model/LoginResponse.cs:76 at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395

我已经在 this 文件中添加了完整的异常详细信息。

我的代码

ViewModel

EventAllItems.Add(eventshb); //异常在这一行触发

public Events events;
private ObservableCollection<eventsHB> _eventAllItems;
public ObservableCollection<eventsHB> EventAllItems
{
    get
    {
        return _eventAllItems;
    }
    set
    {
        _eventAllItems = value;
        OnPropertyChanged("EventAllItems");
    }
}

var Response = await client.GetAsync("my rest api path");
if (Response.IsSuccessStatusCode)
{
    string response = await Response.Content.ReadAsStringAsync();
    events = new Events();
    if (response != "")
    {
        events = JsonConvert.DeserializeObject<Events>(response.ToString());
        foreach (var eventshb in events.eventsHB)
        {
            foreach(var events in eventshb.eventsList)
            {
                eventshb.eventsListTO = events;
                EventAllItems.Add(eventshb); //The exception is triggering on this line 
            }
            lastDateInList = eventshb.day.ToString();
        }
    }
}

型号Class

public class Events
{
    public List<eventsHB> eventsHB { get; set; }
}

public class eventsHB
{
    public long day { get; set; }
    public List<EventsList> eventsList { get; set; }
    public EventsList eventsListTO { get; set; }
}

public class EventsList
{
    public string title { get; set; }
    public string description { get; set; }
    public string longDescription { get; set; }
    public string startDate { get; set; }
    public string endDate { get; set; }
    public string startTime { get; set; }
    public string endTime { get; set; }
}

示例数据:

{
    "eventsHB": [
        {
            "day": 1627914600000,
            "eventsList": [
                {
                    "calendarEventId": 236,
                    "calenderId": 948154,
                    "title": "Business Meet",
                    "description": "SD July 2 ED July 5",
                    "longDescription": "SD July 2 ED July 5"
                }
            ]
        },
        {
            "day": 1628001000000,
            "eventsList": [
                {
                    "calendarEventId": 236,
                    "calenderId": 948154,
                    "title": "Business Meet",
                    "description": "SD July 2 ED July 5",
                    "longDescription": "SD July 2 ED July 5"
                }
            ]
        },
        {
            "day": 1628087400000,
            "eventsList": [
                {
                    "calendarEventId": 236,
                    "calenderId": 948154,
                    "title": "Business Meet",
                    "description": "SD July 2 ED July 5",
                    "longDescription": "SD July 2 ED July 5"
            ]
        }
    ]
}

Xaml:我正在使用 eventsListTO 变量来显示 UI.

的详细信息
<ListView 
    x:Name="EventsListview"
    SeparatorVisibility="None"
    ItemsSource="{Binding EventAllItems, Mode=TwoWay}"
    HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout
                        VerticalOptions="CenterAndExpand"
                        Margin="10,5,10,0"
                        Orientation="Vertical">

                        <Frame
                            VerticalOptions="CenterAndExpand"
                            HasShadow="False">

                            <StackLayout
                                VerticalOptions="CenterAndExpand"
                                Orientation="Vertical">

                                <Label
                                    Text="{Binding eventsListTO.title}"
                                    TextColor="#1C7DB4"
                                    HorizontalOptions="Start" 
                                    VerticalOptions="Center">
                                    <Label.FontFamily>
                                        <OnPlatform x:TypeArguments="x:String">
                                            <On Platform="iOS" Value="Roboto-Medium" />
                                            <On Platform="Android" Value="Roboto-Medium.ttf#Roboto-Medium" />
                                            <On Platform="UWP" Value="Assets/Fonts/Roboto-Medium.ttf#Roboto" />
                                        </OnPlatform>
                                    </Label.FontFamily>
                                    <Label.FontSize>
                                        <OnIdiom x:TypeArguments="x:Double">
                                            <OnIdiom.Phone>17</OnIdiom.Phone>
                                            <OnIdiom.Tablet>25</OnIdiom.Tablet>
                                            <OnIdiom.Desktop>17</OnIdiom.Desktop>
                                        </OnIdiom>
                                    </Label.FontSize>
                                </Label>

                                
                                    <Label 
                                        Text="{Binding eventsListTO.description}"
                                        TextColor="#1C7DB4"
                                        VerticalTextAlignment="Center"
                                        Margin="0,-3,-5,0"
                                        HorizontalOptions="FillAndExpand" 
                                        VerticalOptions="Center">
                                        <Label.FontFamily>
                                            <OnPlatform x:TypeArguments="x:String">
                                                <On Platform="iOS" Value="Roboto-Light" />
                                                <On Platform="Android" Value="Roboto-Light.ttf#Roboto-Light" />
                                                <On Platform="UWP" Value="Assets/Fonts/Roboto-Light.ttf#Roboto" />
                                            </OnPlatform>
                                        </Label.FontFamily>
                                        <Label.FontSize>
                                            <OnIdiom x:TypeArguments="x:Double">
                                                <OnIdiom.Phone>12</OnIdiom.Phone>
                                                <OnIdiom.Tablet>18</OnIdiom.Tablet>
                                                <OnIdiom.Desktop>12</OnIdiom.Desktop>
                                            </OnIdiom>
                                        </Label.FontSize>
                                    </Label>
                                </StackLayout>
                            </StackLayout>
                        </Frame>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.Footer>
        <Label/>
    </ListView.Footer>
</ListView>

我在 UI 上列出了一些项目。异常仅显示在实际 iPhone 设备上。在 android 设备和 iPhone 模拟器上一切正常。

XF版本:4.8.0.1821

Mac 对比版本:8.10.2

Xcode版本:12.5.1

iPhone 版本:14.6、14.4

问题出在日期和时间值上。

日期问题:在 Android 设备和 ios 模拟器上,日期格式为 dd/mm/yyyy。但是在实际的 iPhone 设备上,日期格式是 dd-mm-yyyy.

时间问题:在 ios 模拟器上,我们得到时间的 AM 或 PM 值,但在实际 iPhone 设备上,时间是 24 小时格式,没有 AM 或 PM。