初始化后Xamarin页面未被更新

时间:2018-03-11 18:21:38

标签: c# xamarin httpclient

您好,我对Xamarin相对较新,我正在面对我的Xamarin.iOS项目的问题,内容页面由于某些原因而未被更新,我不知道,它有点令人困惑至少我。已经检索到的徽标和通过调用我们服务器上的web api计算出的值,没有一个显示为空白内容页面。我试图通过在检测web api服务器上计算的图像和值的方法中放置一个断点来调试错误,并且在这样做时,我在Application Output Window中看到了这条消息。

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. 

我注意到的一个奇怪的事情是,如果我不添加断点,应用程序将继续运行而不会抛出任何异常或崩溃,但是没有任何意义,因为我只是面对的是没有数据和空白的ui。

以下是我用来检索数据的方法:

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代码:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

使用BeginInvokeOnMainThread并在OnApearing方法中调用它

Device.BeginInvokeOnMainThread (LoadCards);

答案 1 :(得分:0)

我明白了,这是网址中查询字符串的问题,该值并非来自正确的来源,所以基本上不是:

Settings.RestUrl + "/api/Customer/GetCustomerCards?CustomerID=" + Helpers.Settings.CustID;

我有这个:

Settings.RestUrl + "/api/Customer/GetCustomerCards?CustomerID=" + Helpers.Settings.storecustID;

当我将断点更深入到方法中时,断点没有击中的原因是因为带有return关键字的if statement,但仍然让我感到困惑的是,虽然单步执行app在我成功进入if statement之前曾经崩溃​​,这使得弄清问题​​的最初原因变得更加困难。