【问题标题】:Xamarin forms My List View Display a Blank PageXamarin 表单我的列表视图显示空白页
【发布时间】:2022-02-02 16:57:22
【问题描述】:

json

   [
                    {
                        "_id": "5a6736a55c720e4593f74236",
                        "indigentApplicationDetails": {
                            "conditionsDetail": {
                                "foodDetail": {
                                    "rating": {}
                                },
                                "clothingDetail": {
                                    "rating": {}
                                },
                                "medicalDetail": {
                                    "rating": {}
                                },
                                "shelterDetail": {
                                    "rating": {}
                                }
                            },
                            "householdDetail": [
                                {
                                    "currentApplicationRefNo": "1000048573",
                                    "personDetail": {
                                        "gender": "Male",
                                        "surname": "Khabanya",
                                        "initials": "S G",
                                        "genderDisplay": "Male",
                                        "personID": "125555",
                                        "firstNames": "Siphiwo Gift",
                                        "title": "37",
                                        "relationship": "Other",
                                        "titleDisplay": "Mr",
                                        "idNo": "6706115835080",
                                        "birthDate": "1967-06-11 00:00:00"
                                    },
                                    "incomeDetail": {
                                        "amount": "0",
                                        "budgetDetail": {
                                            "otherIncome": "0",
                                            "totalPersonIncome": "0",
                                            "accountHolder": "true",
                                            "propertyRenting": "0",
                                            "uif": "0",
                                            "previousWorkPension": "0",
                                            "homeBusiness": "0.0",
                                            "oldAgePension": "0",
                                            "disabilityPension": "0.0"
                                        }
                                    },
                                    "workSituationDetail": {
                                        "skillCurrentDetail": {
                                            "skillDetail": {}
                                        },
                                        "employmentDetail": {},
                                        "skillDesiredDetail": {
                                            "skillDetail": {}
                                        },
                                        "workDuration": "0"
                                    },
                                    "healthDetail": {
                                        "mentalDefect": "false",
                                        "disability": "false",
                                        "poorHealthDetails": "0"
                                    }
                                }
                            ]

列表视图

<ListView
                                HasUnevenRows="True"
                                ItemsSource="{Binding ItemsSource}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Label TextColor="White" Text="{Binding IndigentApplicationDetails.householdDetail.currentApplicationRe‌​fNo}"/>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

代码

public partial class MainPage : ContentPage
                {
                    public MainPage()
                    {
                        InitializeComponent();
                        BindingContext = new IndigentDetailsViewModels();
                    }

                    protected override async void OnAppearing()
                    {
                        base.OnAppearing();
                        await (BindingContext as IndigentDetailsViewModels)?.LoadData();
                    }
                }

         class IndigentDetailsViewModels : INotifyPropertyChanged
            {
                private List<IndigentApplicationDetails> _indigentDetails;

                public List<IndigentApplicationDetails> IndigentDetails
                {
                    get { return _indigentDetails; }
                    set
                    {
                        _indigentDetails = value;
                        OnPropertyChanged(nameof(IndigentDetails));
                    }
                }


                public IndigentDetailsViewModels()
                {
                    _indigentDetailsServices = new indigentDetailsServices();
                }

                readonly indigentDetailsServices _indigentDetailsServices;

                public async Task LoadData()
                {
                    IndigentDetails = await _indigentDetailsServices.GetIndigentDetailsAsync();
                }

                public event PropertyChangedEventHandler PropertyChanged;

                // [NotifyPropertyChangedInvocator]
                protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }


    class indigentDetailsServices
        {
            HttpClient client = new HttpClient();

            public async Task<List<IndigentApplicationDetails>> GetIndigentDetailsAsync()
            {

                try
                {
                    var response = await client.GetStringAsync("https://munipoiapp.herokuapp.com/api/applications/New/KemptonMobileward1");
                    var IndigentDetails = JsonConvert.DeserializeObject<List<IndigentApplicationDetails>>(response);
                    return IndigentDetails;
                }
                catch (System.Exception exception)
                {
                    return null;
                }
            }

        }


 class IndigentApplicationDetails
    {
        public int currentApplicationRefNo { get; set; }
    }

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    您在屏幕上看不到任何内容的原因有几个:

    1. 错误的绑定上下文:

    您正在使用ItemsSource="{Binding ItemsSource}",但您的IndigentDetailsViewModels 中没有名为ItemsSource 的此类属性。你应该绑定IndigentDetails

    1. 细胞绑定错误:

    Text="{Binding IndigentApplicationDetails.householdDetail.currentApplicationRe‌​fNo}" 应该是Text="{Binding currentApplicationRe‌​fNo}"

    1. 文本颜色错误:

    大多数平台的单元格背景颜色为白色。如果您将文本颜色设为白色,它将是不可见的。

    【讨论】:

      【解决方案2】:

      请检查下面的 xaml 代码并检查它是否适合您

       <ListView HasUnevenRows="True" ItemsSource="{Binding IndigentDetails}">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <ViewCell>
                          <StackLayout Padding="10">
                             <Label Text="{Binding currentApplicationRe‌​fNo}"/>
                          </StackLayout>
                     </ViewCell>
                 </DataTemplate>
             </ListView.ItemTemplate>
          </ListView>
      

      【讨论】:

        猜你喜欢
        • 2018-07-18
        • 1970-01-01
        • 2020-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-03
        • 2022-01-03
        相关资源
        最近更新 更多