0

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

未处理的异常:System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.FormatException:输入字符串的格式不正确。在 /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System 中的 System.Number.ThrowOverflowOrFormatException(System.Boolean 溢出,System.String overflowResourceKey)[0x00000]。 Private.CoreLib/shared/System/Number.Parsing.cs:1781 在 System.Number.ParseInt32(System.ReadOnlySpan`1[T] 值,System.Globalization.NumberStyles 样式,System.Globalization.NumberFormatInfo 信息)[0x00016] /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 (系统。

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

我的代码

视图模型

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();
        }
    }
}

模型类

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 VS 版本:8.10.2

Xcode 版本:12.5.1

iPhone 版本:14.6、14.4

4

1 回答 1

0

问题在于日期和时间值。

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

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

于 2021-08-04T15:18:47.207 回答