【问题标题】:Xamarin Page Not Being Updated After InitializationXamarin 页面在初始化后未更新
【发布时间】: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


【解决方案1】:

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

Device.BeginInvokeOnMainThread (LoadCards);

【讨论】:

    【解决方案2】:

    我想通了,这是 url 中的查询字符串有问题,该值不是从正确的来源获取的,所以基本上不是:

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

    我有这个:

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

    当我将断点更深地放置在方法内部时,断点没有命中的原因是 if statement 带有 return 关键字,但仍然让我感到困惑的是,在单步执行该方法时应用程序曾经崩溃过我什至设法进入了if statement,这使得找出问题的最初原因变得更加困难。

    【讨论】:

    • 该应用程序不会显示任何内容,因为您不会返回任何内容并且您正在使用 try catch(它真的不会崩溃,调试您的 catch 语句也许您会看到错误)。也许您应该实例化您的列表并返回它,默认情况下不返回任何内容。另外,如果您满足 if 语句中的条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多