【发布时间】:2018-03-11 18:21:38
【问题描述】:
您好,我对 xamarin 比较陌生,我的 Xamarin.iOS 项目遇到了问题,由于某种我不知道的原因,内容页面没有更新,至少对我来说有点困惑。检索到的徽标和通过调用我们服务器上的 web api 计算的值,没有显示只是空白的内容页面。我尝试通过在检索 Web api 服务器上计算的图像和值的方法中放置一个断点来调试错误,然后在应用程序输出窗口中看到此消息。
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
我注意到的奇怪的事情是,如果我不添加断点,应用程序将继续运行而不会抛出任何异常或崩溃,但这没有意义,因为我面临的只是没有数据和空白用户界面。
这是我用来检索数据的方法:
public async void LoadCards()
{
try
{
List<Requests.Cards> cd = new List<Requests.Cards>();
var content = "";
var RestUrl = Settings.RestUrl + "/api/Customer/GetCustomerCards?CustomerID=" + Helpers.Settings.storecustID;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(RestUrl);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Giftworx-App", Helpers.Settings.Usertoken);
if (Helpers.Settings.Usertoken == string.Empty || Helpers.Settings.storecustID == 0)
{
return;
}
HttpResponseMessage response = await client.GetAsync(RestUrl);
content = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<List<Requests.Cards>>(content);
CardBalance.ItemsSource = Items;
foreach (var i in Items)
{
StorePic.Source = i.CustomerLogo;
}
}
catch (Exception ex)
{
string exception = ex.Message;
}
}
这是 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" x:Class="LoyaltyWorx.TabbedPages.Abraham">
<ContentPage.Content>
<Grid>
<Image Source="NewBg.jpg" Aspect="AspectFill"/>
<StackLayout Orientation="Vertical"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand"
Padding="10, 10, 5, 10">
<StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand" >
<Image HeightRequest="200" WidthRequest="200" HorizontalOptions="CenterAndExpand" x:Name="StorePic"/>
</StackLayout>
<BoxView HeightRequest="5" />
<StackLayout Orientation="Vertical" BackgroundColor="#33ffffff">
<StackLayout
HorizontalOptions="Center"
Orientation="Horizontal"
VerticalOptions="CenterAndExpand"
>
<StackLayout HorizontalOptions="Center" Orientation="Vertical">
<BoxView HeightRequest="10" />
<Label
Text="LOYALTY CARD BALANCE"
HorizontalOptions="Center"
TextColor="White"
VerticalOptions="CenterAndExpand" />
<ListView x:Name="CardBalance" HasUnevenRows="True" VerticalOptions="FillAndExpand" IsPullToRefreshEnabled="True" Refreshing="CardBalance_Refreshing" ItemTapped="CardBalance_ItemTapped" SeparatorVisibility="None" BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="Layout"
>
<Label
Text="{Binding Balance, StringFormat='{0:N2}'}}"
FontAttributes="Bold"
FontSize="40"
HorizontalOptions="Center"
TextColor="White"
VerticalOptions="CenterAndExpand" />
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" Spacing="25">
<Image Source="point.png" WidthRequest="19" HeightRequest="19" />
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" Spacing="20">
<Label Text="Tap to view detail" TextColor="White" FontSize="12"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</StackLayout>
<BoxView HeightRequest="5" />
<StackLayout Orientation="Vertical">
<StackLayout
HorizontalOptions="Center"
Orientation="Horizontal"
VerticalOptions="CenterAndExpand">
<StackLayout HorizontalOptions="Center" Orientation="Vertical">
<Button Text="PROMOTIONS" TextColor="White" HeightRequest="80" WidthRequest="300" FontAttributes="Bold" FontSize="25" BorderColor="White" BorderWidth="2" BorderRadius="40" BackgroundColor="#1Affffff" Clicked="Button_Clicked"/>
<Button Text="TRANSACTIONS" TextColor="White" WidthRequest="300" FontAttributes="Bold" FontSize="15" BorderColor="White" BorderWidth="2" BorderRadius="30" BackgroundColor="#1Affffff" Clicked="Button_Clicked_1"/>
<Button Text="RECEIPTS" TextColor="White" WidthRequest="300" FontAttributes="Bold" FontSize="15" BorderColor="White" BorderWidth="2" BorderRadius="30" BackgroundColor="#1Affffff" IsVisible="false"/>
<BoxView HeightRequest="40" />
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>
【问题讨论】:
-
请贴出相关代码
-
相关代码贴出
-
这似乎是 Xamarin Forms,而不是 X.iOS。您确定您的数据已成功加载并且没有抛出异常吗? ContentPage 的 XAML 或代码是什么样的?
-
您正在访问 UI 控件。您可能不会从 UI 线程访问它们。从代码中无法判断,但这是最好的猜测。
-
@Jason 我发布了 Xaml 代码。伊万,我会试一试,然后告诉你进展如何。
标签: c# xamarin httpclient